2026-08-26 17:05:17 +03:00
|
|
|
// Code generated by internal codegen utility; DO NOT EDIT.
|
|
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/url"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func SelectHeaderContentType(contentTypes []string) string {
|
|
|
|
|
if len(contentTypes) == 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2026-09-28 11:14:36 +03:00
|
|
|
|
2026-08-26 17:05:17 +03:00
|
|
|
if contains(contentTypes, "application/json") {
|
|
|
|
|
return "application/json"
|
|
|
|
|
}
|
2026-09-28 11:14:36 +03:00
|
|
|
|
2026-08-26 17:05:17 +03:00
|
|
|
return contentTypes[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SelectHeaderAccept(accepts []string) string {
|
|
|
|
|
if len(accepts) == 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if contains(accepts, "application/json") {
|
|
|
|
|
return "application/json"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strings.Join(accepts, ",")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func contains(haystack []string, needle string) bool {
|
|
|
|
|
for _, value := range haystack {
|
|
|
|
|
if strings.EqualFold(value, needle) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-28 11:14:36 +03:00
|
|
|
|
2026-08-26 17:05:17 +03:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ParameterValueToString(obj any) string {
|
|
|
|
|
if obj == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if actualObj, ok := obj.(interface{ GetActualInstanceValue() any }); ok {
|
|
|
|
|
return fmt.Sprintf("%v", actualObj.GetActualInstanceValue())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value := reflect.ValueOf(obj)
|
|
|
|
|
for value.Kind() == reflect.Interface || value.Kind() == reflect.Ptr {
|
|
|
|
|
if value.IsNil() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2026-09-28 11:14:36 +03:00
|
|
|
|
2026-08-26 17:05:17 +03:00
|
|
|
value = value.Elem()
|
|
|
|
|
current := value.Interface()
|
|
|
|
|
|
|
|
|
|
if actualObj, ok := current.(interface{ GetActualInstanceValue() any }); ok {
|
|
|
|
|
return fmt.Sprintf("%v", actualObj.GetActualInstanceValue())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-28 11:14:36 +03:00
|
|
|
if t, ok := value.Interface().(time.Time); ok {
|
|
|
|
|
return t.Format(time.RFC3339Nano)
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-26 17:05:17 +03:00
|
|
|
return fmt.Sprintf("%v", value.Interface())
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-28 11:14:36 +03:00
|
|
|
func ParameterArrayToString(obj any, collectionType string) string {
|
|
|
|
|
value := reflect.ValueOf(obj)
|
|
|
|
|
for value.Kind() == reflect.Interface || value.Kind() == reflect.Ptr {
|
|
|
|
|
if value.IsNil() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
value = value.Elem()
|
|
|
|
|
}
|
|
|
|
|
if !value.IsValid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strings.Join(parameterArrayValues(value), collectionDelimiter(collectionType))
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-26 17:05:17 +03:00
|
|
|
func ParameterAddToHeaderOrQuery(headerOrQueryParams any, keyPrefix string, obj any, style string, collectionType string) {
|
|
|
|
|
if obj == nil {
|
2026-09-28 11:14:36 +03:00
|
|
|
addParameterValue(headerOrQueryParams, keyPrefix, "null")
|
2026-08-26 17:05:17 +03:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value := reflect.ValueOf(obj)
|
|
|
|
|
if !value.IsValid() {
|
2026-09-28 11:14:36 +03:00
|
|
|
addParameterValue(headerOrQueryParams, keyPrefix, "invalid")
|
2026-08-26 17:05:17 +03:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for value.Kind() == reflect.Interface || value.Kind() == reflect.Ptr {
|
|
|
|
|
if value.IsNil() {
|
2026-09-28 11:14:36 +03:00
|
|
|
addParameterValue(headerOrQueryParams, keyPrefix, "null")
|
2026-08-26 17:05:17 +03:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
value = value.Elem()
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-28 11:14:36 +03:00
|
|
|
switch value.Kind() {
|
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
|
if value.Len() == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if style == "deepObject" {
|
|
|
|
|
for i := 0; i < value.Len(); i++ {
|
|
|
|
|
itemKeyPrefix := keyPrefix + "[" + strconv.Itoa(i) + "]"
|
|
|
|
|
ParameterAddToHeaderOrQuery(headerOrQueryParams, itemKeyPrefix, value.Index(i).Interface(), style, collectionType)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
items := parameterArrayValues(value)
|
|
|
|
|
if collectionType == "multi" {
|
|
|
|
|
if values, ok := headerOrQueryParams.(url.Values); ok {
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
values.Add(keyPrefix, item)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addParameterValue(headerOrQueryParams, keyPrefix, strings.Join(items, collectionDelimiter(collectionType)))
|
|
|
|
|
|
|
|
|
|
case reflect.Map:
|
|
|
|
|
if value.IsNil() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iter := value.MapRange()
|
|
|
|
|
for iter.Next() {
|
|
|
|
|
mapKey := fmt.Sprintf("%v", iter.Key().Interface())
|
|
|
|
|
ParameterAddToHeaderOrQuery(headerOrQueryParams, fmt.Sprintf("%s[%s]", keyPrefix, mapKey), iter.Value().Interface(), style, collectionType)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
addParameterValue(headerOrQueryParams, keyPrefix, parameterScalarValue(value.Interface()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parameterArrayValues(value reflect.Value) []string {
|
|
|
|
|
items := make([]string, value.Len())
|
|
|
|
|
for i := range items {
|
|
|
|
|
items[i] = parameterScalarValue(value.Index(i).Interface())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return items
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parameterScalarValue(obj any) string {
|
|
|
|
|
if obj == nil {
|
|
|
|
|
return "null"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value := reflect.ValueOf(obj)
|
|
|
|
|
for value.Kind() == reflect.Interface || value.Kind() == reflect.Ptr {
|
|
|
|
|
if value.IsNil() {
|
|
|
|
|
return "null"
|
|
|
|
|
}
|
|
|
|
|
value = value.Elem()
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-26 17:05:17 +03:00
|
|
|
switch value.Kind() {
|
|
|
|
|
case reflect.Struct:
|
|
|
|
|
if t, ok := value.Interface().(time.Time); ok {
|
2026-09-28 11:14:36 +03:00
|
|
|
return t.Format(time.RFC3339Nano)
|
2026-08-26 17:05:17 +03:00
|
|
|
}
|
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2026-09-28 11:14:36 +03:00
|
|
|
return strconv.FormatInt(value.Int(), 10)
|
2026-08-26 17:05:17 +03:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2026-09-28 11:14:36 +03:00
|
|
|
return strconv.FormatUint(value.Uint(), 10)
|
2026-08-26 17:05:17 +03:00
|
|
|
case reflect.Float32:
|
2026-09-28 11:14:36 +03:00
|
|
|
return strconv.FormatFloat(value.Float(), 'g', -1, 32)
|
2026-08-26 17:05:17 +03:00
|
|
|
case reflect.Float64:
|
2026-09-28 11:14:36 +03:00
|
|
|
return strconv.FormatFloat(value.Float(), 'g', -1, 64)
|
2026-08-26 17:05:17 +03:00
|
|
|
case reflect.Bool:
|
2026-09-28 11:14:36 +03:00
|
|
|
return strconv.FormatBool(value.Bool())
|
2026-08-26 17:05:17 +03:00
|
|
|
case reflect.String:
|
2026-09-28 11:14:36 +03:00
|
|
|
return value.String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value.Type().String() + " value"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectionDelimiter(collectionType string) string {
|
|
|
|
|
switch collectionType {
|
|
|
|
|
case "ssv":
|
|
|
|
|
return " "
|
|
|
|
|
case "tsv":
|
|
|
|
|
return "\t"
|
|
|
|
|
case "pipes":
|
|
|
|
|
return "|"
|
2026-08-26 17:05:17 +03:00
|
|
|
default:
|
2026-09-28 11:14:36 +03:00
|
|
|
// Swagger 2 defaults to CSV.
|
|
|
|
|
return ","
|
2026-08-26 17:05:17 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-28 11:14:36 +03:00
|
|
|
func addParameterValue(headerOrQueryParams any, keyPrefix, value string) {
|
2026-08-26 17:05:17 +03:00
|
|
|
switch values := headerOrQueryParams.(type) {
|
|
|
|
|
case url.Values:
|
2026-09-28 11:14:36 +03:00
|
|
|
values.Add(keyPrefix, value)
|
2026-08-26 17:05:17 +03:00
|
|
|
case map[string]string:
|
|
|
|
|
values[keyPrefix] = value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ParameterToJSON(obj any) (string, error) {
|
|
|
|
|
jsonBuf, err := json.Marshal(obj)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2026-09-28 11:14:36 +03:00
|
|
|
|
2026-08-26 17:05:17 +03:00
|
|
|
return string(jsonBuf), err
|
|
|
|
|
}
|