Skip to content

Commit 17d85e3

Browse files
committed
Allow typed slices via reflection
1 parent ed99db2 commit 17d85e3

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

result.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (fk *FieldKey) Field() string {
8282
}
8383

8484
// NewItemKey returns a pair of a slice and index usable as a key of a map.
85-
func NewItemKey(slice []interface{}, i int) ItemKey {
85+
func NewItemKey(slice interface{}, i int) ItemKey {
8686
return ItemKey{slice: reflect.ValueOf(slice), index: i}
8787
}
8888

@@ -103,7 +103,7 @@ type fieldSchemata struct {
103103
}
104104

105105
type itemSchemata struct {
106-
slice []interface{}
106+
slice reflect.Value
107107
index int
108108
schemata schemata
109109
}
@@ -196,15 +196,15 @@ func (r *Result) mergeForField(obj map[string]interface{}, field string, other *
196196
}
197197

198198
// mergeForSlice merges other into r, assigning other's root schemata to the given slice and index.
199-
func (r *Result) mergeForSlice(slice []interface{}, i int, other *Result) *Result {
199+
func (r *Result) mergeForSlice(slice reflect.Value, i int, other *Result) *Result {
200200
if other == nil {
201201
return r
202202
}
203203
r.mergeWithoutRootSchemata(other)
204204

205205
if other.rootObjectSchemata.Len() > 0 {
206206
if r.itemSchemata == nil {
207-
r.itemSchemata = make([]itemSchemata, len(slice))
207+
r.itemSchemata = make([]itemSchemata, slice.Len())
208208
}
209209
r.itemSchemata = append(r.itemSchemata, itemSchemata{
210210
slice: slice,
@@ -233,9 +233,9 @@ func (r *Result) addPropertySchemata(obj map[string]interface{}, fld string, sch
233233

234234
// addSliceSchemata adds the given schemata for the slice and index.
235235
// The slice schemata might be reused. I.e. do not modify it after being added to a result.
236-
func (r *Result) addSliceSchemata(slice []interface{}, i int, schema *spec.Schema) {
236+
func (r *Result) addSliceSchemata(slice reflect.Value, i int, schema *spec.Schema) {
237237
if r.itemSchemata == nil {
238-
r.itemSchemata = make([]itemSchemata, 0, len(slice))
238+
r.itemSchemata = make([]itemSchemata, 0, slice.Len())
239239
}
240240
r.itemSchemata = append(r.itemSchemata, itemSchemata{slice: slice, index: i, schemata: schemata{one: schema}})
241241
}

slice_validator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (s *schemaSliceValidator) Validate(data interface{}) *Result {
5757
for i := 0; i < size; i++ {
5858
validator.SetPath(fmt.Sprintf("%s.%d", s.Path, i))
5959
value := val.Index(i)
60-
result.mergeForSlice(data.([]interface{}), i, validator.Validate(value.Interface()))
60+
result.mergeForSlice(val, i, validator.Validate(value.Interface()))
6161
}
6262
}
6363

@@ -69,7 +69,7 @@ func (s *schemaSliceValidator) Validate(data interface{}) *Result {
6969
if val.Len() <= i {
7070
break
7171
}
72-
result.mergeForSlice(data.([]interface{}), int(i), validator.Validate(val.Index(i).Interface()))
72+
result.mergeForSlice(val, int(i), validator.Validate(val.Index(i).Interface()))
7373
}
7474
}
7575
if s.AdditionalItems != nil && itemsSize < size {
@@ -79,7 +79,7 @@ func (s *schemaSliceValidator) Validate(data interface{}) *Result {
7979
if s.AdditionalItems.Schema != nil {
8080
for i := itemsSize; i < size-itemsSize+1; i++ {
8181
validator := NewSchemaValidator(s.AdditionalItems.Schema, s.Root, fmt.Sprintf("%s.%d", s.Path, i), s.KnownFormats)
82-
result.mergeForSlice(data.([]interface{}), int(i), validator.Validate(val.Index(int(i)).Interface()))
82+
result.mergeForSlice(val, int(i), validator.Validate(val.Index(int(i)).Interface()))
8383
}
8484
}
8585
}

0 commit comments

Comments
 (0)