Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenlu committed Jul 10, 2024
1 parent 2591a8b commit f6acc61
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
28 changes: 14 additions & 14 deletions uma/test/tlv_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (b *BinaryCodableStruct) UnmarshalBytes(data []byte) error {

type TLVUtilsTests struct {
StringField string `tlv:"0"`
IntField int `tlv:"1"`
BoolField bool `tlv:"2"`
IntField int `tlv:"1"`
BoolField bool `tlv:"2"`
UInt64Field uint64 `tlv:"3"`
}

Expand All @@ -37,8 +37,8 @@ func (d *TLVUtilsTests) UnmarshalTLV(data []byte) error {
func TestSimpleTLVCoder(t *testing.T) {
tlvUtilsTests := TLVUtilsTests{
StringField: "hello",
IntField: 42,
BoolField: true,
IntField: 42,
BoolField: true,
UInt64Field: 123,
}

Expand Down Expand Up @@ -71,24 +71,24 @@ func TestSimpleTLVCoder(t *testing.T) {
}

type NestedTLVUtilsTests struct {
StringField string `tlv:"0"`
IntField int `tlv:"1"`
BoolField bool `tlv:"2"`
UInt64Field uint64 `tlv:"3"`
NestedField TLVUtilsTests `tlv:"4"`
StringField string `tlv:"0"`
IntField int `tlv:"1"`
BoolField bool `tlv:"2"`
UInt64Field uint64 `tlv:"3"`
NestedField TLVUtilsTests `tlv:"4"`
BinaryCodableField BinaryCodableStruct `tlv:"5"`
}

func TestNestedTLVCoder(t *testing.T) {
nestedTLVUtilsTests := NestedTLVUtilsTests{
StringField: "hello",
IntField: 42,
BoolField: true,
IntField: 42,
BoolField: true,
UInt64Field: 123,
NestedField: TLVUtilsTests{
StringField: "world",
IntField: 43,
BoolField: false,
IntField: 43,
BoolField: false,
UInt64Field: 124,
},
BinaryCodableField: BinaryCodableStruct{
Expand Down Expand Up @@ -147,4 +147,4 @@ func TestNestedTLVCoder(t *testing.T) {
t.Fatalf("expected binary codable field to be 'binary', got %s", nestedTLVUtilsTests2.BinaryCodableField.Data)
}

}
}
40 changes: 20 additions & 20 deletions uma/utils/tlv_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ type TLVCodable interface {
// The "tlv" tag value will be the type of the field.
func MarshalTLV(v interface{}) ([]byte, error) {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Struct {
if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Struct {
return nil, fmt.Errorf("marshal requires a pointer to a struct")
}
}

val = reflect.Indirect(val)
typ := val.Type()
typ := val.Type()

var result []byte
for i := 0; i < val.NumField(); i++ {
Expand Down Expand Up @@ -89,27 +89,27 @@ func MarshalTLV(v interface{}) ([]byte, error) {
func UnmarshalTLV(v interface{}, data []byte) error {
result := make(map[byte][]byte)
for i := 0; i < len(data); {
if i+2 > len(data) {
return fmt.Errorf("incomplete TLV at position %d", i)
}
t := data[i]
l := data[i+1]
if i+2+int(l) > len(data) {
return fmt.Errorf("incomplete value for type %d at position %d", t, i)
}
v := data[i+2 : i+2+int(l)]
result[t] = v
i += 2 + int(l)
}
if i+2 > len(data) {
return fmt.Errorf("incomplete TLV at position %d", i)
}

t := data[i]
l := data[i+1]

if i+2+int(l) > len(data) {
return fmt.Errorf("incomplete value for type %d at position %d", t, i)
}

v := data[i+2 : i+2+int(l)]
result[t] = v

i += 2 + int(l)
}

val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Struct {
return fmt.Errorf("unmarshal requires a pointer to a struct")
}
}
val = reflect.Indirect(val)
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
Expand Down

0 comments on commit f6acc61

Please sign in to comment.