Skip to content

Commit

Permalink
fix: closes #390 by returning an error when parsing slices or arrays …
Browse files Browse the repository at this point in the history
…containing Variable within heterogenous objects
  • Loading branch information
gbotrel committed Jan 13, 2023
1 parent 3a03e4e commit fbd8de0
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion frontend/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package schema

import (
"errors"
"fmt"
"io"
"reflect"
Expand Down Expand Up @@ -386,11 +387,17 @@ func parse(r []Field, input interface{}, target reflect.Type, parentFullName, pa
// nothing to add
return r, nil
}
// ensure the subfields are the same, we don't support heterogenous or arrays
for i := 1; i < len(subFields); i++ {
if !consistentField(subFields[0], subFields[i]) {
return nil, errors.New("heterogenous slices or arrays are not supported")
}
}
return append(r, Field{
Name: parentGoName,
NameTag: parentTagName,
Type: Array,
SubFields: subFields[:1], // TODO @gbotrel we should ensure that elements are not heterogeneous?
SubFields: subFields[:1],
Visibility: parentVisibility,
ArraySize: tValue.Len(),
}), nil
Expand All @@ -400,6 +407,40 @@ func parse(r []Field, input interface{}, target reflect.Type, parentFullName, pa
return r, nil
}

func consistentField(f1, f2 Field) bool {
if f1.Type != f2.Type {
return false
}
switch f1.Type {
case Array:
if f1.ArraySize != f2.ArraySize {
return false
}
if (f1.SubFields == nil) != (f2.SubFields == nil) {
return false
}
if len(f1.SubFields) != 0 {
// array max subfield len == 1
return consistentField(f1.SubFields[0], f2.SubFields[0])
}
return true
case Struct:
if len(f1.SubFields) != len(f2.SubFields) {
return false
}
for i, s := range f1.SubFields {
if !consistentField(s, f2.SubFields[i]) {
return false
}
}
return true
case Leaf:
return true
default:
panic("not implemented")
}
}

// specify parentName, name and tag
// returns fully qualified name
func getFullName(parentFullName, name, tagName string) string {
Expand Down

0 comments on commit fbd8de0

Please sign in to comment.