Skip to content

Commit d05ffc8

Browse files
committed
feat: not support list
1 parent 3d66376 commit d05ffc8

5 files changed

Lines changed: 27 additions & 373 deletions

File tree

trim/assign.go

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ func assignValue(desc *Descriptor, src interface{}, destValue reflect.Value, opt
160160
switch desc.Kind {
161161
case TypeKind_Struct:
162162
return assignStruct(desc, src, destValue, opt)
163-
case TypeKind_List:
164-
return assignList(desc, src, destValue, opt)
165163
case TypeKind_StrMap:
166164
return assignStrMap(desc, src, destValue, opt)
167165
default:
@@ -281,71 +279,6 @@ func assignValueToField(desc *Descriptor, src interface{}, fieldValue reflect.Va
281279
return assignValue(desc, src, fieldValue, opt)
282280
}
283281

284-
// assignList handles TypeKind_List assignment
285-
func assignList(desc *Descriptor, src interface{}, destValue reflect.Value, opt *AssignOptions) error {
286-
srcSlice, ok := src.([]interface{})
287-
if !ok {
288-
// Try to handle typed slices
289-
srcValue := reflect.ValueOf(src)
290-
if srcValue.Kind() != reflect.Slice && srcValue.Kind() != reflect.Array {
291-
return fmt.Errorf("expected slice for list, got %T", src)
292-
}
293-
// Convert to []interface{}
294-
srcSlice = make([]interface{}, srcValue.Len())
295-
for i := 0; i < srcValue.Len(); i++ {
296-
srcSlice[i] = srcValue.Index(i).Interface()
297-
}
298-
}
299-
300-
if destValue.Kind() != reflect.Slice {
301-
return fmt.Errorf("expected slice destination, got %v", destValue.Kind())
302-
}
303-
304-
// Find wildcard or indexed descriptors
305-
var wildcardDesc *Descriptor
306-
indexDescMap := make(map[int]*Descriptor, len(desc.Children))
307-
for i := range desc.Children {
308-
child := &desc.Children[i]
309-
if child.Name == "*" {
310-
wildcardDesc = child.Desc
311-
} else {
312-
indexDescMap[child.ID] = child.Desc
313-
}
314-
}
315-
316-
// Create a new slice with the same length as src
317-
newSlice := reflect.MakeSlice(destValue.Type(), len(srcSlice), len(srcSlice))
318-
319-
for i, elem := range srcSlice {
320-
if elem == nil {
321-
continue
322-
}
323-
324-
elemValue := newSlice.Index(i)
325-
326-
// Find the appropriate descriptor
327-
var childDesc *Descriptor
328-
if d, ok := indexDescMap[i]; ok {
329-
childDesc = d
330-
} else {
331-
childDesc = wildcardDesc
332-
}
333-
334-
if childDesc != nil {
335-
if err := assignValueToField(childDesc, elem, elemValue, opt); err != nil {
336-
return err
337-
}
338-
} else {
339-
if err := assignScalar(elem, elemValue); err != nil {
340-
return err
341-
}
342-
}
343-
}
344-
345-
destValue.Set(newSlice)
346-
return nil
347-
}
348-
349282
// assignStrMap handles TypeKind_StrMap assignment
350283
func assignStrMap(desc *Descriptor, src interface{}, destValue reflect.Value, opt *AssignOptions) error {
351284
srcMap, ok := src.(map[string]interface{})

trim/assign_test.go

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestAssignAny_NestedStruct(t *testing.T) {
126126

127127
func TestAssignAny_List(t *testing.T) {
128128
src := map[string]interface{}{
129-
"field_list": []interface{}{1, 2, 3},
129+
"field_list": []int{1, 2, 3},
130130
}
131131

132132
desc := &Descriptor{
@@ -137,11 +137,8 @@ func TestAssignAny_List(t *testing.T) {
137137
Name: "field_list",
138138
ID: 6,
139139
Desc: &Descriptor{
140-
Kind: TypeKind_List,
140+
Kind: TypeKind_Scalar,
141141
Name: "LIST",
142-
Children: []Field{
143-
{Name: "*"},
144-
},
145142
},
146143
},
147144
},
@@ -282,27 +279,28 @@ func TestAssignAny_UnknownFields(t *testing.T) {
282279
}
283280

284281
func TestAssignAny_ListOfStructs(t *testing.T) {
282+
285283
src := map[string]interface{}{
286-
"field_b": []interface{}{
287-
map[string]interface{}{
288-
"field_a": 1,
289-
"field_e": "first",
284+
"field_b": []*SampleAssign{
285+
{
286+
FieldA: intPtr(1),
287+
FieldE: "first",
290288
},
291-
map[string]interface{}{
292-
"field_a": 2,
293-
"field_e": "second",
289+
{
290+
FieldA: intPtr(2),
291+
FieldE: "second",
294292
},
295293
},
296294
}
297295

298-
nestedDesc := &Descriptor{
299-
Kind: TypeKind_Struct,
300-
Name: "SampleAssign",
301-
Children: []Field{
302-
{Name: "field_a", ID: 1},
303-
{Name: "field_e", ID: 5},
304-
},
305-
}
296+
// nestedDesc := &Descriptor{
297+
// Kind: TypeKind_Struct,
298+
// Name: "SampleAssign",
299+
// Children: []Field{
300+
// {Name: "field_a", ID: 1},
301+
// {Name: "field_e", ID: 5},
302+
// },
303+
// }
306304

307305
desc := &Descriptor{
308306
Kind: TypeKind_Struct,
@@ -312,11 +310,8 @@ func TestAssignAny_ListOfStructs(t *testing.T) {
312310
Name: "field_b",
313311
ID: 2,
314312
Desc: &Descriptor{
315-
Kind: TypeKind_List,
313+
Kind: TypeKind_Scalar,
316314
Name: "LIST",
317-
Children: []Field{
318-
{Name: "*", Desc: nestedDesc},
319-
},
320315
},
321316
},
322317
},
@@ -474,9 +469,8 @@ func BenchmarkAssignAny(b *testing.B) {
474469
Name: "field_list",
475470
ID: 6,
476471
Desc: &Descriptor{
477-
Kind: TypeKind_List,
478-
Name: "LIST",
479-
Children: []Field{{Name: "*"}},
472+
Kind: TypeKind_Scalar,
473+
Name: "LIST",
480474
},
481475
},
482476
{

trim/desc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import (
2424
type TypeKind int
2525

2626
const (
27-
TypeKind_Struct TypeKind = iota + 1
27+
TypeKind_Scalar TypeKind = iota
28+
TypeKind_Struct
2829
TypeKind_StrMap
29-
TypeKind_List
30+
// TypeKind_List
3031
)
3132

3233
// Descriptor describes the entire a DSL-pruning scheme for a type.
@@ -40,7 +41,6 @@ type Descriptor struct {
4041
Name string
4142

4243
// children for TypeKind_Struct|TypeKind_StrMap|TypeKind_List
43-
// - For TypeKind_List, there is either one Field with Name "*" or index as ID
4444
// - For TypeKind_StrMap, either each Field is a key-value pair or one field with Name "*"
4545
// - For TypeKind_Struct, each Field is a field with both Name and ID
4646
Children []Field
@@ -52,7 +52,7 @@ type Field struct {
5252
// Or the selection key for TypeKind_StrMap
5353
Name string
5454

55-
// IDL-FieldID or Array-Index
55+
// FieldID in IDL
5656
ID int
5757

5858
// the child of the field

trim/fetch.go

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,6 @@ func fetchValue(desc *Descriptor, v reflect.Value, opt *FetchOptions) (interface
128128
case TypeKind_Struct:
129129
return fetchStruct(desc, v, opt)
130130

131-
case TypeKind_List:
132-
return fetchList(desc, v, opt)
133-
134131
case TypeKind_StrMap:
135132
return fetchStrMap(desc, v, opt)
136133

@@ -271,37 +268,6 @@ func fetchUnknownValue(val interface{}, desc *Descriptor) interface{} {
271268
}
272269
return result
273270

274-
case TypeKind_List:
275-
// ReadAny returns []interface{} for LIST type
276-
listVal, ok := val.([]interface{})
277-
if !ok {
278-
return val
279-
}
280-
281-
// Find wildcard or indexed descriptors
282-
var wildcardDesc *Descriptor
283-
indexDescMap := make(map[int]*Descriptor, len(desc.Children))
284-
for i := range desc.Children {
285-
child := &desc.Children[i]
286-
if child.Name == "*" {
287-
wildcardDesc = child.Desc
288-
} else {
289-
indexDescMap[child.ID] = child.Desc
290-
}
291-
}
292-
293-
result := make([]interface{}, len(listVal))
294-
for i, elem := range listVal {
295-
if childDesc, ok := indexDescMap[i]; ok {
296-
result[i] = fetchUnknownValue(elem, childDesc)
297-
} else if wildcardDesc != nil {
298-
result[i] = fetchUnknownValue(elem, wildcardDesc)
299-
} else {
300-
result[i] = elem
301-
}
302-
}
303-
return result
304-
305271
case TypeKind_StrMap:
306272
// ReadAny returns map[string]interface{} for string-keyed MAP type
307273
strMap, ok := val.(map[string]interface{})
@@ -338,91 +304,6 @@ func fetchUnknownValue(val interface{}, desc *Descriptor) interface{} {
338304
}
339305
}
340306

341-
// fetchList handles TypeKind_List
342-
func fetchList(desc *Descriptor, v reflect.Value, opt *FetchOptions) (interface{}, error) {
343-
kind := v.Kind()
344-
if kind != reflect.Slice && kind != reflect.Array {
345-
return nil, nil
346-
}
347-
348-
childrenLen := len(desc.Children)
349-
vLen := v.Len()
350-
351-
// Fast path: only wildcard descriptor
352-
if childrenLen == 1 && desc.Children[0].Name == "*" {
353-
wildcardDesc := desc.Children[0].Desc
354-
result := make([]interface{}, 0, vLen)
355-
for i := 0; i < vLen; i++ {
356-
elem := v.Index(i)
357-
if elem.Kind() == reflect.Ptr && elem.IsNil() {
358-
result = append(result, nil)
359-
continue
360-
}
361-
if wildcardDesc != nil {
362-
fetched, err := fetchValue(wildcardDesc, elem, opt)
363-
if err != nil {
364-
return nil, err
365-
}
366-
result = append(result, fetched)
367-
} else {
368-
result = append(result, elem.Interface())
369-
}
370-
}
371-
return result, nil
372-
}
373-
374-
// Build a map of index -> descriptor for quick lookup
375-
indexDescMap := make(map[int]*Field, childrenLen)
376-
var wildcardDesc *Field
377-
for i := range desc.Children {
378-
child := &desc.Children[i]
379-
if child.Name == "*" {
380-
wildcardDesc = child
381-
} else {
382-
// Field.ID represents the slice index
383-
indexDescMap[child.ID] = child
384-
}
385-
}
386-
387-
// Check if specific indices are requested but not available
388-
if opt.DisallowNotFound {
389-
for idx := range indexDescMap {
390-
if idx >= vLen {
391-
return nil, ErrNotFound{Parent: desc, Field: indexDescMap[idx], Msg: fmt.Sprintf("index %d out of range (len=%d)", idx, vLen)}
392-
}
393-
}
394-
}
395-
396-
result := make([]interface{}, 0, vLen)
397-
for i := 0; i < vLen; i++ {
398-
elem := v.Index(i)
399-
if elem.Kind() == reflect.Ptr && elem.IsNil() {
400-
result = append(result, nil)
401-
continue
402-
}
403-
404-
// First try to find descriptor by index (Field.ID)
405-
var childDesc *Descriptor
406-
if field, ok := indexDescMap[i]; ok && field.Desc != nil {
407-
childDesc = field.Desc
408-
} else if wildcardDesc != nil && wildcardDesc.Desc != nil {
409-
// Fallback to wildcard descriptor
410-
childDesc = wildcardDesc.Desc
411-
}
412-
413-
if childDesc != nil {
414-
fetched, err := fetchValue(childDesc, elem, opt)
415-
if err != nil {
416-
return nil, err
417-
}
418-
result = append(result, fetched)
419-
} else {
420-
result = append(result, elem.Interface())
421-
}
422-
}
423-
return result, nil
424-
}
425-
426307
// fetchStrMap handles TypeKind_StrMap
427308
func fetchStrMap(desc *Descriptor, v reflect.Value, opt *FetchOptions) (interface{}, error) {
428309
if v.Kind() != reflect.Map || v.Type().Key().Kind() != reflect.String {

0 commit comments

Comments
 (0)