MCPcopy
hub / github.com/expr-lang/expr / copyExportedFields

Function copyExportedFields

internal/testify/assert/assertions.go:84–145  ·  view source on GitHub ↗

copyExportedFields iterates downward through nested data structures and creates a copy that only contains the exported struct fields.

(expected interface{})

Source from the content-addressed store, hash-verified

82// copyExportedFields iterates downward through nested data structures and creates a copy
83// that only contains the exported struct fields.
84func copyExportedFields(expected interface{}) interface{} {
85 if isNil(expected) {
86 return expected
87 }
88
89 expectedType := reflect.TypeOf(expected)
90 expectedKind := expectedType.Kind()
91 expectedValue := reflect.ValueOf(expected)
92
93 switch expectedKind {
94 case reflect.Struct:
95 result := reflect.New(expectedType).Elem()
96 for i := 0; i < expectedType.NumField(); i++ {
97 field := expectedType.Field(i)
98 isExported := field.IsExported()
99 if isExported {
100 fieldValue := expectedValue.Field(i)
101 if isNil(fieldValue) || isNil(fieldValue.Interface()) {
102 continue
103 }
104 newValue := copyExportedFields(fieldValue.Interface())
105 result.Field(i).Set(reflect.ValueOf(newValue))
106 }
107 }
108 return result.Interface()
109
110 case reflect.Ptr:
111 result := reflect.New(expectedType.Elem())
112 unexportedRemoved := copyExportedFields(expectedValue.Elem().Interface())
113 result.Elem().Set(reflect.ValueOf(unexportedRemoved))
114 return result.Interface()
115
116 case reflect.Array, reflect.Slice:
117 var result reflect.Value
118 if expectedKind == reflect.Array {
119 result = reflect.New(reflect.ArrayOf(expectedValue.Len(), expectedType.Elem())).Elem()
120 } else {
121 result = reflect.MakeSlice(expectedType, expectedValue.Len(), expectedValue.Len())
122 }
123 for i := 0; i < expectedValue.Len(); i++ {
124 index := expectedValue.Index(i)
125 if isNil(index) {
126 continue
127 }
128 unexportedRemoved := copyExportedFields(index.Interface())
129 result.Index(i).Set(reflect.ValueOf(unexportedRemoved))
130 }
131 return result.Interface()
132
133 case reflect.Map:
134 result := reflect.MakeMap(expectedType)
135 for _, k := range expectedValue.MapKeys() {
136 index := expectedValue.MapIndex(k)
137 unexportedRemoved := copyExportedFields(index.Interface())
138 result.SetMapIndex(k, reflect.ValueOf(unexportedRemoved))
139 }
140 return result.Interface()
141

Callers 3

TestCopyExportedFieldsFunction · 0.85
EqualExportedValuesFunction · 0.85

Calls 4

isNilFunction · 0.85
ElemMethod · 0.80
SetMethod · 0.80
LenMethod · 0.45

Tested by 1

TestCopyExportedFieldsFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…