Skip to content

Commit

Permalink
Fix fixed array encode
Browse files Browse the repository at this point in the history
  • Loading branch information
freehere107 committed Mar 7, 2024
1 parent a914e5c commit 908ae59
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion types/FixedArray.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (f *FixedArray) Encode(value interface{}) string {
case reflect.String:
valueStr := value.(string)
if strings.HasPrefix(valueStr, "0x") {
return valueStr
return utiles.TrimHex(valueStr)
} else {
return utiles.BytesToHex([]byte(valueStr))
}
Expand Down
12 changes: 10 additions & 2 deletions types/FixedU8.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ func (s *FixedU8) Process() {
}
}

func (s *FixedU8) Encode(value string) string {
return utiles.TrimHex(value)
func (s *FixedU8) Encode(value interface{}) string {
switch value.(type) {
case string:
return utiles.TrimHex(value.(string))
case []byte:
return utiles.TrimHex(utiles.BytesToHex(value.([]byte)))
default:
panic("type error,only support string or []byte")
}
return ""
}

func (s *FixedU8) TypeStructString() string {
Expand Down
2 changes: 2 additions & 0 deletions types/Uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func (u *U8) Encode(value interface{}) string {
i = int(v)
case float64:
i = int(v)
case uint8:
i = int(v)
}
return utiles.U8Encode(i)
}
Expand Down
7 changes: 7 additions & 0 deletions types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,10 @@ func TestBitVec(t *testing.T) {
assert.EqualValues(t, r[i], m.ProcessAndUpdateData("BitVec"))
}
}

func TestFixedArray(t *testing.T) {
ts := []string{"[u8; 16]", "[u8; 16]", "[u8;16]"}
for i, v := range []interface{}{[]byte{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, []byte{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, "0x02020202020202020202020202020202"} {
assert.Equal(t, Encode(ts[i], v), "02020202020202020202020202020202", "TestFixedArray Encode fail %s", ts[i])
}
}

0 comments on commit 908ae59

Please sign in to comment.