mirror of
https://github.com/LukeHagar/plexterraform.git
synced 2025-12-06 20:47:43 +00:00
ci: regenerated with OpenAPI Doc 0.0.3, Speakeasy CLI 1.129.1
This commit is contained in:
162
internal/sdk/pkg/utils/utils.go
Normal file
162
internal/sdk/pkg/utils/utils.go
Normal file
@@ -0,0 +1,162 @@
|
||||
// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math/big"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ericlagergren/decimal"
|
||||
)
|
||||
|
||||
const (
|
||||
queryParamTagKey = "queryParam"
|
||||
headerParamTagKey = "header"
|
||||
pathParamTagKey = "pathParam"
|
||||
)
|
||||
|
||||
var (
|
||||
paramRegex = regexp.MustCompile(`({.*?})`)
|
||||
SerializationMethodToContentType = map[string]string{
|
||||
"json": "application/json",
|
||||
"form": "application/x-www-form-urlencoded",
|
||||
"multipart": "multipart/form-data",
|
||||
"raw": "application/octet-stream",
|
||||
"string": "text/plain",
|
||||
}
|
||||
)
|
||||
|
||||
func UnmarshalJsonFromResponseBody(body io.Reader, out interface{}, tag string) error {
|
||||
data, err := io.ReadAll(body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading response body: %w", err)
|
||||
}
|
||||
if err := UnmarshalJSON(data, out, reflect.StructTag(tag), true, false); err != nil {
|
||||
return fmt.Errorf("error unmarshalling json response body: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ReplaceParameters(stringWithParams string, params map[string]string) string {
|
||||
if len(params) == 0 {
|
||||
return stringWithParams
|
||||
}
|
||||
|
||||
return paramRegex.ReplaceAllStringFunc(stringWithParams, func(match string) string {
|
||||
match = match[1 : len(match)-1]
|
||||
return params[match]
|
||||
})
|
||||
}
|
||||
|
||||
func Contains(slice []string, item string) bool {
|
||||
for _, s := range slice {
|
||||
if s == item {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func parseStructTag(tagKey string, field reflect.StructField) map[string]string {
|
||||
tag := field.Tag.Get(tagKey)
|
||||
if tag == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
values := map[string]string{}
|
||||
|
||||
options := strings.Split(tag, ",")
|
||||
for _, optionConf := range options {
|
||||
parts := strings.Split(optionConf, "=")
|
||||
|
||||
switch len(parts) {
|
||||
case 1:
|
||||
// flag option
|
||||
parts = append(parts, "true")
|
||||
case 2:
|
||||
// key=value option
|
||||
default:
|
||||
// invalid option
|
||||
continue
|
||||
}
|
||||
|
||||
values[parts[0]] = parts[1]
|
||||
}
|
||||
|
||||
return values
|
||||
}
|
||||
|
||||
func parseParamTag(tagKey string, field reflect.StructField, defaultStyle string, defaultExplode bool) *paramTag {
|
||||
// example `{tagKey}:"style=simple,explode=false,name=apiID"`
|
||||
values := parseStructTag(tagKey, field)
|
||||
if values == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
tag := ¶mTag{
|
||||
Style: defaultStyle,
|
||||
Explode: defaultExplode,
|
||||
ParamName: strings.ToLower(field.Name),
|
||||
}
|
||||
|
||||
for k, v := range values {
|
||||
switch k {
|
||||
case "style":
|
||||
tag.Style = v
|
||||
case "explode":
|
||||
tag.Explode = v == "true"
|
||||
case "name":
|
||||
tag.ParamName = v
|
||||
case "serialization":
|
||||
tag.Serialization = v
|
||||
}
|
||||
}
|
||||
|
||||
return tag
|
||||
}
|
||||
|
||||
func valToString(val interface{}) string {
|
||||
switch v := val.(type) {
|
||||
case time.Time:
|
||||
return v.Format(time.RFC3339Nano)
|
||||
case big.Int:
|
||||
return v.String()
|
||||
case decimal.Big:
|
||||
return v.String()
|
||||
default:
|
||||
return fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
|
||||
func populateFromGlobals(fieldType reflect.StructField, valType reflect.Value, paramType string, globals map[string]map[string]map[string]interface{}) reflect.Value {
|
||||
if globals != nil && fieldType.Type.Kind() == reflect.Ptr {
|
||||
parameters, ok := globals["parameters"]
|
||||
if ok {
|
||||
paramsOfType, ok := parameters[paramType]
|
||||
if ok {
|
||||
globalVal, ok := paramsOfType[fieldType.Name]
|
||||
if ok {
|
||||
if reflect.TypeOf(globalVal).Kind() == fieldType.Type.Elem().Kind() && valType.IsNil() {
|
||||
valType = reflect.ValueOf(&globalVal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return valType
|
||||
}
|
||||
|
||||
func isNil(typ reflect.Type, val reflect.Value) bool {
|
||||
if typ.Kind() == reflect.Ptr || typ.Kind() == reflect.Map || typ.Kind() == reflect.Slice || typ.Kind() == reflect.Interface {
|
||||
return val.IsNil()
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user