Go API Documentation

github.com/TykTechnologies/tyk/internal/reflect

No package summary is available.

Package

Files: 3. Third party imports: 1. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Types

FlatMap

FlatMap is alias of map[string]any.

Field name Field type Comment
type

map[string]any

No comment on field.
type FlatMap map[string]any

Functions

func Cast

Cast converts a value of type any to a specified type T. It does this by first marshaling the source value to JSON, and then unmarshaling the JSON byte slice into the destination type T.

This function can be useful when dealing with dynamic or untyped data, such as data obtained from external sources or user input.

The function returns a pointer to the converted value of type *T, and an error value if the conversion fails.

Example:

type Person struct {
	Name string
	Age  int
}

data := map[string]any{
	"Name": "Alice",
	"Age":  30,
}

var p Person
pptr, err := Cast[Person](data)
if err != nil {
	// Handle error
}
p = *pptr

Note: The Cast function assumes that the source value can be marshaled and unmarshaled as JSON. If the source value contains types or values that cannot be represented in JSON, the function will return an error.

func Cast[T any](src any) (*T, error) {
	var dst T
	b, err := json.Marshal(src)
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal(b, &dst)
	if err != nil {
		return nil, err
	}
	return &dst, nil
}

Cognitive complexity: 4, Cyclomatic complexity: 3

Uses: json.Marshal, json.Unmarshal.

func Clone

Clone is a hacky way to wrap the generic declaration. Using var Clone = clone.Clone is not allowed.

func Clone[T any](t T) T {
	return clone.Clone[T](t)
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: clone.Clone.

func Flatten

Flatten transforms deep map to flat map. The numeric types are coalesced to float64.

func Flatten(data map[string]any) (flatmap FlatMap, err error) {
	flatmap = make(FlatMap)
	for k, raw := range data {
		err = flatten(flatmap, k, reflect.ValueOf(raw))
		if err != nil {
			return nil, err
		}
	}
	return
}

Cognitive complexity: 5, Cyclomatic complexity: 3

Uses: reflect.ValueOf.

func IsEmpty

IsEmpty checks whether a field should be set to empty and omitted from OAS JSON.

func IsEmpty(i interface{}) bool {
	return IsZero(reflect.ValueOf(i))
}

Cognitive complexity: 1, Cyclomatic complexity: 1

Uses: reflect.ValueOf.

func IsZero

IsZero is a customized implementation of reflect.Value.IsZero. The built-in function accepts slice, map and pointer fields having 0 length as not zero. In OAS, we would like them to be counted as empty so we separated slice, map and pointer to different cases.

func IsZero(v reflect.Value) bool {
	switch v.Kind() {
	case reflect.Bool:
		return !v.Bool()
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return v.Int() == 0
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return v.Uint() == 0
	case reflect.Float32, reflect.Float64:
		return math.Float64bits(v.Float()) == 0
	case reflect.Complex64, reflect.Complex128:
		c := v.Complex()
		return math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0
	case reflect.Array:
		for i := 0; i < v.Len(); i++ {
			if !IsZero(v.Index(i)) {
				return false
			}
		}
		return true
	case reflect.Chan, reflect.Func, reflect.Interface, reflect.UnsafePointer:
		return v.IsNil()
	case reflect.Ptr:
		return v.IsNil() || IsZero(v.Elem())
	case reflect.Slice, reflect.Map:
		return v.Len() == 0
	case reflect.String:
		return v.Len() == 0
	case reflect.Struct:
		for i := 0; i < v.NumField(); i++ {
			if !IsZero(v.Field(i)) {
				return false
			}
		}
		return true
	default:
		// This should never happens, but will act as a safeguard for
		// later, as a default value doesn't makes sense here.
		panic(&reflect.ValueError{Method: "oas.IsZero", Kind: v.Kind()})
	}
}

Cognitive complexity: 22, Cyclomatic complexity: 19

Uses: math.Float64bits, reflect.Array, reflect.Bool, reflect.Chan, reflect.Complex128, reflect.Complex64, reflect.Float32, reflect.Float64, reflect.Func, reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice, reflect.String, reflect.Struct, reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8, reflect.Uintptr, reflect.UnsafePointer, reflect.ValueError.

Private functions

func flatten

unlike maps.Flatten, this flatten coalesces numeric types to a float64 value. this is used in yaml decoding to map[]any as a numeric type.

flatten (result FlatMap, prefix string, v reflect.Value) error
References: fmt.Errorf, reflect.Array, reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8, reflect.Interface, reflect.Invalid, reflect.Map, reflect.Slice, reflect.String, reflect.Struct, reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8.

func flattenMap

flattenMap (result FlatMap, prefix string, v reflect.Value) error
References: fmt.Sprintf, reflect.Interface, reflect.String.

func flattenSliceArray

flattenSliceArray (result FlatMap, prefix string, v reflect.Value) error
References: fmt.Sprintf.

func flattenStruct

flattenStruct (result FlatMap, prefix string, v reflect.Value) error
References: fmt.Sprintf.


Tests

Files: 1. Third party imports: 1. Imports from organisation: 0. Tests: 2. Benchmarks: 0.

Types

subStruct

This type doesn't have documentation.

Field name Field type Comment
SubMap

map[string]string

No comment on field.
type subStruct struct {
	SubMap map[string]string
}

testStruct

This type doesn't have documentation.

Field name Field type Comment
Bool

bool

No comment on field.
Array

[]string

No comment on field.
Map

map[string]string

No comment on field.
SubStruct

*subStruct

No comment on field.
type testStruct struct {
	Bool		bool
	Array		[]string
	Map		map[string]string
	SubStruct	*subStruct
}

Test functions

Test_Cast

References: assert.Equal, assert.Error, assert.NoError, testing.T.

Test_IsEmpty

References: assert.False, assert.True.