Skip to content

Commit

Permalink
implements write arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
siscia committed Mar 8, 2022
1 parent 486ed60 commit 4526361
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ua/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func encode(val reflect.Value, name string) ([]byte, error) {
return writeStruct(val, name)
case reflect.Slice:
return writeSlice(val, name)
case reflect.Array:
return writeArray(val, name)
default:
return nil, errors.Errorf("unsupported type: %s", val.Type())
}
Expand Down Expand Up @@ -136,3 +138,24 @@ func writeSlice(val reflect.Value, name string) ([]byte, error) {
}
return buf.Bytes(), buf.Error()
}

func writeArray(val reflect.Value, name string) ([]byte, error) {
buf := NewBuffer(nil)

if val.Len() > math.MaxInt32 {
return nil, errors.Errorf("array too large")
}

buf.WriteUint32(uint32(val.Len()))

// loop over elements
for i := 0; i < val.Len(); i++ {
ename := fmt.Sprintf("%s[%d]", name, i)
b, err := encode(val.Index(i), ename)
if err != nil {
return nil, err
}
buf.Write(b)
}
return buf.Bytes(), buf.Error()
}

0 comments on commit 4526361

Please sign in to comment.