v1.0.0
This commit is contained in:
commit
e7f605ec8c
292 files changed
+43992
No files matched your search
@@ -0,0 +1,268 @@
|
||||
// Code generated by internal codegen utility; DO NOT EDIT.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"repository.basistech.ru/BASIS/de-golang-sdk/core"
|
||||
)
|
||||
|
||||
var (
|
||||
jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?json)`)
|
||||
xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?xml)`)
|
||||
queryParamSplit = regexp.MustCompile(`(^|&)([^&]+)`)
|
||||
queryParamUnescaper = strings.NewReplacer("%5B", "[", "%5D", "]")
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
cfg *core.Configuration
|
||||
}
|
||||
|
||||
func NewClient(cfg *core.Configuration) *Client {
|
||||
if cfg == nil {
|
||||
cfg = core.NewConfiguration()
|
||||
}
|
||||
if cfg.HTTPClient == nil {
|
||||
cfg.HTTPClient = http.DefaultClient
|
||||
}
|
||||
|
||||
return &Client{cfg: cfg}
|
||||
}
|
||||
|
||||
func (c *Client) BaseURL() string {
|
||||
return strings.TrimRight(c.cfg.ServerURL, "/")
|
||||
}
|
||||
|
||||
func (c *Client) CallAPI(request *http.Request, _ bool) (*http.Response, error) {
|
||||
return c.callAPIWithHTTPClient(c.cfg.HTTPClient, request)
|
||||
}
|
||||
|
||||
func (c *Client) CallAPIWithoutRedirects(request *http.Request) (*http.Response, error) {
|
||||
httpClient := *c.cfg.HTTPClient
|
||||
httpClient.CheckRedirect = func(*http.Request, []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
}
|
||||
|
||||
return c.callAPIWithHTTPClient(&httpClient, request)
|
||||
}
|
||||
|
||||
func (c *Client) callAPIWithHTTPClient(httpClient *http.Client, request *http.Request) (*http.Response, error) {
|
||||
resp, err := httpClient.Do(request)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
type FormFile struct {
|
||||
FieldName string
|
||||
Filename string
|
||||
Reader io.Reader
|
||||
}
|
||||
|
||||
func (c *Client) PrepareRequest(
|
||||
ctx context.Context,
|
||||
path string,
|
||||
method string,
|
||||
requestBody any,
|
||||
headerParams map[string]string,
|
||||
queryParams url.Values,
|
||||
formParams url.Values,
|
||||
formFiles []FormFile,
|
||||
) (*http.Request, error) {
|
||||
if ctx == nil {
|
||||
return nil, errors.New("nil context")
|
||||
}
|
||||
for _, formFile := range formFiles {
|
||||
if formFile.Filename == "" {
|
||||
return nil, fmt.Errorf("multipart file %q has an empty filename", formFile.FieldName)
|
||||
}
|
||||
if formFile.Reader == nil {
|
||||
return nil, fmt.Errorf("multipart file %q has a nil reader", formFile.FieldName)
|
||||
}
|
||||
}
|
||||
|
||||
var body io.Reader
|
||||
var closeMultipart func(error)
|
||||
|
||||
if requestBody != nil {
|
||||
contentType := headerParams["Content-Type"]
|
||||
if contentType == "" {
|
||||
contentType = detectContentType(requestBody)
|
||||
headerParams["Content-Type"] = contentType
|
||||
}
|
||||
|
||||
var err error
|
||||
body, err = setBody(requestBody, contentType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if (strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0) || len(formFiles) > 0 {
|
||||
if body != nil {
|
||||
return nil, errors.New("cannot specify requestBody and multipart form at the same time.")
|
||||
}
|
||||
pipeReader, pipeWriter := io.Pipe()
|
||||
writer := multipart.NewWriter(pipeWriter)
|
||||
headerParams["Content-Type"] = writer.FormDataContentType()
|
||||
|
||||
body = pipeReader
|
||||
go func() {
|
||||
var err error
|
||||
defer func() {
|
||||
pipeWriter.CloseWithError(err)
|
||||
}()
|
||||
|
||||
for key, values := range formParams {
|
||||
for _, value := range values {
|
||||
err = writer.WriteField(key, value)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, formFile := range formFiles {
|
||||
var part io.Writer
|
||||
part, err = writer.CreateFormFile(formFile.FieldName, filepath.Base(formFile.Filename))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = io.Copy(part, formFile.Reader); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
err = writer.Close()
|
||||
}()
|
||||
closeMultipart = func(err error) {
|
||||
pipeReader.CloseWithError(err)
|
||||
pipeWriter.CloseWithError(err)
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 {
|
||||
if body != nil {
|
||||
return nil, errors.New("cannot specify requestBody and x-www-form-urlencoded form at the same time.")
|
||||
}
|
||||
formBody := &bytes.Buffer{}
|
||||
if _, err := formBody.WriteString(formParams.Encode()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body = formBody
|
||||
headerParams["Content-Length"] = fmt.Sprintf("%d", formBody.Len())
|
||||
}
|
||||
|
||||
pathParsed, err := url.Parse(path)
|
||||
if err != nil {
|
||||
if closeMultipart != nil {
|
||||
closeMultipart(err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query := pathParsed.Query()
|
||||
for key, values := range queryParams {
|
||||
for _, value := range values {
|
||||
query.Add(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
pathParsed.RawQuery = queryParamSplit.ReplaceAllStringFunc(query.Encode(), func(value string) string {
|
||||
pieces := strings.Split(value, "=")
|
||||
pieces[0] = queryParamUnescaper.Replace(pieces[0])
|
||||
return strings.Join(pieces, "=")
|
||||
})
|
||||
|
||||
var request *http.Request
|
||||
if body != nil {
|
||||
request, err = http.NewRequestWithContext(ctx, method, pathParsed.String(), body)
|
||||
} else {
|
||||
request, err = http.NewRequestWithContext(ctx, method, pathParsed.String(), nil)
|
||||
}
|
||||
if err != nil {
|
||||
if closeMultipart != nil {
|
||||
closeMultipart(err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(headerParams) > 0 {
|
||||
headers := http.Header{}
|
||||
for header, value := range headerParams {
|
||||
headers[header] = []string{value}
|
||||
}
|
||||
request.Header = headers
|
||||
}
|
||||
|
||||
request.Header.Add("User-Agent", c.cfg.UserAgent)
|
||||
|
||||
for header, value := range c.cfg.DefaultHeaders() {
|
||||
request.Header.Add(header, value)
|
||||
}
|
||||
|
||||
return request, nil
|
||||
}
|
||||
|
||||
func setBody(body any, contentType string) (bodyBuffer *bytes.Buffer, err error) {
|
||||
if bodyBuffer == nil {
|
||||
bodyBuffer = &bytes.Buffer{}
|
||||
}
|
||||
|
||||
if reader, ok := body.(io.Reader); ok {
|
||||
_, err = bodyBuffer.ReadFrom(reader)
|
||||
} else if bytesValue, ok := body.([]byte); ok {
|
||||
_, err = bodyBuffer.Write(bytesValue)
|
||||
} else if stringValue, ok := body.(string); ok {
|
||||
_, err = bodyBuffer.WriteString(stringValue)
|
||||
} else if stringValue, ok := body.(*string); ok {
|
||||
_, err = bodyBuffer.WriteString(*stringValue)
|
||||
} else if jsonCheck.MatchString(contentType) {
|
||||
err = json.NewEncoder(bodyBuffer).Encode(body)
|
||||
} else if xmlCheck.MatchString(contentType) {
|
||||
var encoded []byte
|
||||
encoded, err = xml.Marshal(body)
|
||||
if err == nil {
|
||||
bodyBuffer.Write(encoded)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if bodyBuffer.Len() == 0 {
|
||||
return nil, fmt.Errorf("invalid body type %s\n", contentType)
|
||||
}
|
||||
return bodyBuffer, nil
|
||||
}
|
||||
|
||||
func detectContentType(body any) string {
|
||||
contentType := "text/plain; charset=utf-8"
|
||||
kind := reflect.TypeOf(body).Kind()
|
||||
|
||||
switch kind {
|
||||
case reflect.Struct, reflect.Map, reflect.Ptr:
|
||||
contentType = "application/json; charset=utf-8"
|
||||
case reflect.String:
|
||||
contentType = "text/plain; charset=utf-8"
|
||||
default:
|
||||
if bytesValue, ok := body.([]byte); ok {
|
||||
contentType = http.DetectContentType(bytesValue)
|
||||
} else if kind == reflect.Slice {
|
||||
contentType = "application/json; charset=utf-8"
|
||||
}
|
||||
}
|
||||
|
||||
return contentType
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
// 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 ""
|
||||
}
|
||||
if contains(contentTypes, "application/json") {
|
||||
return "application/json"
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
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 ""
|
||||
}
|
||||
value = value.Elem()
|
||||
current := value.Interface()
|
||||
|
||||
if actualObj, ok := current.(interface{ GetActualInstanceValue() any }); ok {
|
||||
return fmt.Sprintf("%v", actualObj.GetActualInstanceValue())
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%v", value.Interface())
|
||||
}
|
||||
|
||||
func ReplacePathParameter(path, name, value string) string {
|
||||
return strings.ReplaceAll(path, "{"+name+"}", url.PathEscape(value))
|
||||
}
|
||||
|
||||
func ParameterAddToHeaderOrQuery(headerOrQueryParams any, keyPrefix string, obj any, style string, collectionType string) {
|
||||
if obj == nil {
|
||||
addParameterValue(headerOrQueryParams, keyPrefix, "null", collectionType)
|
||||
return
|
||||
}
|
||||
|
||||
value := reflect.ValueOf(obj)
|
||||
if !value.IsValid() {
|
||||
addParameterValue(headerOrQueryParams, keyPrefix, "invalid", collectionType)
|
||||
return
|
||||
}
|
||||
|
||||
for value.Kind() == reflect.Interface || value.Kind() == reflect.Ptr {
|
||||
if value.IsNil() {
|
||||
addParameterValue(headerOrQueryParams, keyPrefix, "null", collectionType)
|
||||
return
|
||||
}
|
||||
value = value.Elem()
|
||||
}
|
||||
|
||||
switch value.Kind() {
|
||||
case reflect.Struct:
|
||||
if t, ok := value.Interface().(time.Time); ok {
|
||||
ParameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, t.Format(time.RFC3339Nano), style, collectionType)
|
||||
return
|
||||
}
|
||||
addParameterValue(headerOrQueryParams, keyPrefix, value.Type().String()+" value", collectionType)
|
||||
case reflect.Slice, reflect.Array:
|
||||
if value.Kind() == reflect.Slice && value.IsNil() {
|
||||
return
|
||||
}
|
||||
for i := 0; i < value.Len(); i++ {
|
||||
itemKeyPrefix := keyPrefix
|
||||
if style == "deepObject" {
|
||||
itemKeyPrefix = keyPrefix + "[" + strconv.Itoa(i) + "]"
|
||||
}
|
||||
ParameterAddToHeaderOrQuery(headerOrQueryParams, itemKeyPrefix, value.Index(i).Interface(), style, 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)
|
||||
}
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
addParameterValue(headerOrQueryParams, keyPrefix, strconv.FormatInt(value.Int(), 10), collectionType)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
addParameterValue(headerOrQueryParams, keyPrefix, strconv.FormatUint(value.Uint(), 10), collectionType)
|
||||
case reflect.Float32:
|
||||
addParameterValue(headerOrQueryParams, keyPrefix, strconv.FormatFloat(value.Float(), 'g', -1, 32), collectionType)
|
||||
case reflect.Float64:
|
||||
addParameterValue(headerOrQueryParams, keyPrefix, strconv.FormatFloat(value.Float(), 'g', -1, 64), collectionType)
|
||||
case reflect.Bool:
|
||||
addParameterValue(headerOrQueryParams, keyPrefix, strconv.FormatBool(value.Bool()), collectionType)
|
||||
case reflect.String:
|
||||
addParameterValue(headerOrQueryParams, keyPrefix, value.String(), collectionType)
|
||||
default:
|
||||
addParameterValue(headerOrQueryParams, keyPrefix, value.Type().String()+" value", collectionType)
|
||||
}
|
||||
}
|
||||
|
||||
func addParameterValue(headerOrQueryParams any, keyPrefix, value, collectionType string) {
|
||||
switch values := headerOrQueryParams.(type) {
|
||||
case url.Values:
|
||||
if collectionType == "csv" && values.Get(keyPrefix) != "" {
|
||||
values.Set(keyPrefix, values.Get(keyPrefix)+","+value)
|
||||
} else {
|
||||
values.Add(keyPrefix, value)
|
||||
}
|
||||
case map[string]string:
|
||||
values[keyPrefix] = value
|
||||
}
|
||||
}
|
||||
|
||||
func ParameterToJSON(obj any) (string, error) {
|
||||
jsonBuf, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(jsonBuf), err
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
// Code generated by internal codegen utility; DO NOT EDIT.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"repository.basistech.ru/BASIS/de-golang-sdk/core"
|
||||
)
|
||||
|
||||
func (c *Client) Decode(v any, body []byte, contentType string) (err error) {
|
||||
if len(body) == 0 {
|
||||
return nil
|
||||
}
|
||||
if stringValue, ok := v.(*string); ok {
|
||||
*stringValue = string(body)
|
||||
return nil
|
||||
}
|
||||
if xmlCheck.MatchString(contentType) {
|
||||
if err = xml.Unmarshal(body, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if jsonCheck.MatchString(contentType) {
|
||||
if actualObj, ok := v.(interface{ GetActualInstance() any }); ok {
|
||||
if unmarshalObj, ok := actualObj.(interface{ UnmarshalJSON([]byte) error }); ok {
|
||||
if err = unmarshalObj.UnmarshalJSON(body); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined")
|
||||
}
|
||||
} else if err = json.Unmarshal(body, v); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = populateEmbeddedAnyMap(v, body); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("undefined response type: %v", contentType)
|
||||
}
|
||||
|
||||
func decodeFileResponseError(body []byte) error {
|
||||
var responseError struct {
|
||||
Message string `json:"message"`
|
||||
Error bool `json:"error"`
|
||||
ErrorCode int64 `json:"errorCode"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &responseError); err != nil || !responseError.Error {
|
||||
return nil
|
||||
}
|
||||
return &core.BackendError{
|
||||
Code: responseError.ErrorCode,
|
||||
Message: responseError.Message,
|
||||
}
|
||||
}
|
||||
|
||||
func ReadResponseBody(resp *http.Response) ([]byte, error) {
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
return body, err
|
||||
}
|
||||
|
||||
func (c *Client) FileResponse(resp *http.Response) (io.ReadCloser, error) {
|
||||
if resp.Body == nil {
|
||||
return nil, errors.New("file response body is nil")
|
||||
}
|
||||
if !jsonCheck.MatchString(resp.Header.Get("Content-Type")) {
|
||||
return resp.Body, nil
|
||||
}
|
||||
|
||||
body, err := ReadResponseBody(resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := decodeFileResponseError(body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return io.NopCloser(bytes.NewReader(body)), nil
|
||||
}
|
||||
|
||||
func populateEmbeddedAnyMap(v any, data []byte) error {
|
||||
return populateEmbeddedAnyMapValue(reflect.ValueOf(v), data)
|
||||
}
|
||||
|
||||
func populateEmbeddedAnyMapValue(value reflect.Value, data []byte) error {
|
||||
if !value.IsValid() {
|
||||
return nil
|
||||
}
|
||||
for value.Kind() == reflect.Ptr || value.Kind() == reflect.Interface {
|
||||
if value.IsNil() {
|
||||
return nil
|
||||
}
|
||||
value = value.Elem()
|
||||
}
|
||||
|
||||
trimmed := bytes.TrimSpace(data)
|
||||
if len(trimmed) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch value.Kind() {
|
||||
case reflect.Struct:
|
||||
if trimmed[0] != '{' {
|
||||
return nil
|
||||
}
|
||||
return populateEmbeddedAnyMapStruct(value, trimmed)
|
||||
case reflect.Slice, reflect.Array:
|
||||
if trimmed[0] != '[' {
|
||||
return nil
|
||||
}
|
||||
var rawItems []json.RawMessage
|
||||
if err := json.Unmarshal(trimmed, &rawItems); err != nil {
|
||||
return err
|
||||
}
|
||||
for i := 0; i < value.Len() && i < len(rawItems); i++ {
|
||||
if err := populateEmbeddedAnyMapValue(value.Index(i), rawItems[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func populateEmbeddedAnyMapStruct(value reflect.Value, data []byte) error {
|
||||
var rawObject map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &rawObject); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if anyField, ok := embeddedAnyMapField(value); ok {
|
||||
if hasJSONKeyFold(rawObject, "any") {
|
||||
return nil
|
||||
}
|
||||
|
||||
var anyValue map[string]any
|
||||
if err := json.Unmarshal(data, &anyValue); err != nil {
|
||||
return err
|
||||
}
|
||||
anyField.Set(reflect.ValueOf(anyValue))
|
||||
return nil
|
||||
}
|
||||
|
||||
valueType := value.Type()
|
||||
for i := 0; i < value.NumField(); i++ {
|
||||
fieldType := valueType.Field(i)
|
||||
if fieldType.PkgPath != "" {
|
||||
continue
|
||||
}
|
||||
rawField, ok := rawJSONField(rawObject, fieldType)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if err := populateEmbeddedAnyMapValue(value.Field(i), rawField); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func embeddedAnyMapField(value reflect.Value) (reflect.Value, bool) {
|
||||
valueType := value.Type()
|
||||
var exportedCount int
|
||||
var anyField reflect.Value
|
||||
|
||||
for i := 0; i < value.NumField(); i++ {
|
||||
fieldType := valueType.Field(i)
|
||||
if fieldType.PkgPath != "" {
|
||||
continue
|
||||
}
|
||||
|
||||
exportedCount++
|
||||
if fieldType.Name == "Any" {
|
||||
anyField = value.Field(i)
|
||||
}
|
||||
}
|
||||
|
||||
if exportedCount != 1 || !anyField.IsValid() || !anyField.CanSet() {
|
||||
return reflect.Value{}, false
|
||||
}
|
||||
|
||||
anyFieldType := anyField.Type()
|
||||
switch anyFieldType.Kind() {
|
||||
case reflect.Interface:
|
||||
case reflect.Map:
|
||||
if anyFieldType.Key().Kind() != reflect.String || anyFieldType.Elem().Kind() != reflect.Interface {
|
||||
return reflect.Value{}, false
|
||||
}
|
||||
default:
|
||||
return reflect.Value{}, false
|
||||
}
|
||||
|
||||
return anyField, true
|
||||
}
|
||||
|
||||
func rawJSONField(rawObject map[string]json.RawMessage, field reflect.StructField) (json.RawMessage, bool) {
|
||||
name := field.Name
|
||||
if tag := field.Tag.Get("json"); tag != "" {
|
||||
tagName := strings.Split(tag, ",")[0]
|
||||
if tagName == "-" {
|
||||
return nil, false
|
||||
}
|
||||
if tagName != "" {
|
||||
name = tagName
|
||||
}
|
||||
}
|
||||
|
||||
if rawField, ok := rawObject[name]; ok {
|
||||
return rawField, true
|
||||
}
|
||||
|
||||
for key, rawField := range rawObject {
|
||||
if strings.EqualFold(key, name) {
|
||||
return rawField, true
|
||||
}
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func hasJSONKeyFold(rawObject map[string]json.RawMessage, name string) bool {
|
||||
for key := range rawObject {
|
||||
if strings.EqualFold(key, name) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// Code generated by internal codegen utility; DO NOT EDIT.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func IsZeroValue(v any) bool {
|
||||
if v == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
rv := reflect.ValueOf(v)
|
||||
switch rv.Kind() {
|
||||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
||||
return rv.Len() == 0
|
||||
case reflect.Bool:
|
||||
return !rv.Bool()
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return rv.Int() == 0
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
return rv.Uint() == 0
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return rv.Float() == 0
|
||||
case reflect.Interface, reflect.Pointer:
|
||||
return rv.IsNil()
|
||||
default:
|
||||
return rv.IsZero()
|
||||
}
|
||||
}
|
||||
|
||||
func IsRequiredParamMissing(v any) bool {
|
||||
if v == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
rv := reflect.ValueOf(v)
|
||||
switch rv.Kind() {
|
||||
case reflect.Bool:
|
||||
return false
|
||||
case reflect.Slice:
|
||||
return rv.IsNil()
|
||||
case reflect.Interface, reflect.Pointer:
|
||||
return rv.IsNil()
|
||||
default:
|
||||
return IsZeroValue(v)
|
||||
}
|
||||
}
|
||||
|
||||
func MissingRequiredParamsError(params []string) error {
|
||||
return fmt.Errorf("required params are missing: %s", strings.Join(params, ", "))
|
||||
}
|
||||
|
||||
func Strlen(s string) int {
|
||||
return utf8.RuneCountInString(s)
|
||||
}
|
||||
Reference in new issue
Block a user