Skip to content

Commit

Permalink
chore: rework/rename some part
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed May 15, 2024
1 parent e6e457e commit 1aebe75
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 50 deletions.
2 changes: 1 addition & 1 deletion gnovm/pkg/gnoamino/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ var Package = amino.RegisterPackage(amino.NewPackage(
"gno",
amino.GetCallersDirname(),
).WithDependencies().WithTypes(
&TypedValueWrapper{},
&TypedValueWrapper{}, "gno_value",
))
10 changes: 4 additions & 6 deletions gnovm/pkg/gnoamino/value_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
)

// TestTypedValueMarshal_Primitive tests marshaling of primitive types.
func TestTypedValueMarshal_Primitive(t *testing.T) {
func TestTypedValueMarshalJSON_Primitive(t *testing.T) {
cases := []struct {
ValueRep string // Go representation
ArgRep string // string representation
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestTypedValueMarshal_Primitive(t *testing.T) {
}

// TestTypedValueMarshal_Array tests marshaling of array types.
func TestTypedValueMarshal_Array(t *testing.T) {
func TestTypedValueMarshalJSON_Array(t *testing.T) {
cases := []struct {
ValueRep string // Go representation
ArgRep string // string representation
Expand Down Expand Up @@ -171,7 +171,7 @@ type Interface struct {
}
`

func TestTypedValueMarshal_Struct(t *testing.T) {
func TestTypedValueMarshalJSON_Struct(t *testing.T) {
cases := []struct {
ValueRepName string // Go representation
ArgRep string // string representation
Expand Down Expand Up @@ -230,7 +230,6 @@ func TestTypedValueMarshal_Struct(t *testing.T) {
require.Len(t, tps, 1)
gt := tps[0].V.(gnolang.TypeValue).Type

// Create Marshaling type
mv := tvm.From(gt)

t.Run("Unmarshal", func(t *testing.T) {
Expand All @@ -239,8 +238,7 @@ func TestTypedValueMarshal_Struct(t *testing.T) {
})

t.Run("Marshal", func(t *testing.T) {
raw, err := amino.MarshalJSONAny(mv)
t.Logf("raw: %s\n", string(raw))
raw, err := amino.MarshalJSON(mv)
require.NoError(t, err)
assert.Equal(t, tc.Expected, string(raw))
})
Expand Down
19 changes: 14 additions & 5 deletions tm2/pkg/amino/binary_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,22 @@ func (cdc *Codec) decodeReflectBinary(bz []byte, info *TypeInfo,

// Handle override if a pointer to rv implements UnmarshalAmino.
if info.IsAminoMarshaler {
// First, decode repr instance from bytes.
rrv := reflect.New(info.ReprType.Type).Elem()
var rrv reflect.Value
var rinfo *TypeInfo
rinfo, err = cdc.getTypeInfoWLock(info.ReprType.Type)
if err != nil {
return

// Try to get TypeInfo if TypeDescription is provided
if info.HasTypeDescription {
rt := typeDescription(rv)

// Use type description as type representation
rinfo, err = cdc.getTypeInfoWLock(rt)
rrv = reflect.New(rt).Elem()
} else {
// Fallback on type repr
rinfo = info.ReprType
rrv = reflect.New(rinfo.Type).Elem()
}

_n, err = cdc.decodeReflectBinary(bz, rinfo, rrv, fopts, bare, options)
if slide(&bz, &n, _n) && err != nil {
return
Expand Down
11 changes: 10 additions & 1 deletion tm2/pkg/amino/binary_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,20 @@ func (cdc *Codec) encodeReflectBinary(w io.Writer, info *TypeInfo, rv reflect.Va
if info.IsAminoMarshaler {
// First, encode rv into repr instance.
var rrv reflect.Value
rinfo := info.ReprType
rrv, err = toReprObject(rv)
if err != nil {
return
}

var rinfo *TypeInfo
if info.HasTypeDescription {
rt := typeDescription(rv)
rrv = rrv.Elem().Convert(rt)
rinfo, err = cdc.getTypeInfoWLock(rt)
} else {
rinfo = info.ReprType
}

// Then, encode the repr instance.
err = cdc.encodeReflectBinary(w, rinfo, rrv, fopts, bare, options)
return
Expand Down
34 changes: 2 additions & 32 deletions tm2/pkg/amino/json_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ func (cdc *Codec) decodeReflectJSON(bz []byte, info *TypeInfo, rv reflect.Value,

// Try to get TypeInfo if TypeDescription is provided
if info.HasTypeDescription {
// XXX: put this in its own method (?)
uwrm := rv.Addr().MethodByName("TypeDesc")
uwouts := uwrm.Call([]reflect.Value{})
rt := uwouts[0].Interface().(reflect.Type)
rt := typeDescription(rv)

// Use type description as type representation
rinfo, err = cdc.getTypeInfoWLock(rt)
Expand Down Expand Up @@ -147,7 +144,6 @@ func invokeStdlibJSONUnmarshal(bz []byte, rv reflect.Value, fopts FieldOptions)
if !rv.CanAddr() && rv.Kind() != reflect.Ptr {
panic("rv not addressable nor pointer")
}

rrv := rv
if rv.Kind() != reflect.Ptr {
rrv = reflect.New(rv.Type())
Expand Down Expand Up @@ -190,35 +186,9 @@ func (cdc *Codec) decodeReflectJSONInterface(bz []byte, iinfo *TypeInfo, rv refl

var value json.RawMessage
var cinfo *TypeInfo
fmt.Printf("%+v\n\n", iinfo.HasTypeDescription)
if iinfo.HasTypeDescription {
// First, decode repr instance from bytes.
// rrv := reflect.New(info.ReprType.Type).Elem()
// rinfo := info.ReprType
// err = cdc.decodeReflectJSON(bz, rinfo, rrv, fopts)
// if err != nil {
// return
// }

// Then, decode from repr instance.
uwrm := rv.Addr().MethodByName("TypeDesc")
uwouts := uwrm.Call([]reflect.Value{})
erri := uwouts[1].Interface()
if erri != nil {
return fmt.Errorf("TypeDesc call error: %w", erri.(error))
}

rt, ok := uwouts[0].Interface().(reflect.Type)
if !ok {
return nil
}

rt := typeDescription(rv)
cinfo, err = cdc.getTypeInfoWLock(rt)
if erri != nil {
return fmt.Errorf("unknown call error: %w", erri.(error))
}

return
} else {
// Extract type_url.
var typeURL string
Expand Down
6 changes: 1 addition & 5 deletions tm2/pkg/amino/json_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ func (cdc *Codec) encodeReflectJSON(w io.Writer, info *TypeInfo, rv reflect.Valu

var rinfo *TypeInfo
if info.HasTypeDescription {
// XXX: put this in its own method
uwrm := rv.Addr().MethodByName("TypeDesc")
uwouts := uwrm.Call([]reflect.Value{})

rt := uwouts[0].Interface().(reflect.Type)
rt := typeDescription(rv)
rrv = rrv.Elem().Convert(rt)

// rrv = reflect.New(info.ReprType.Type).Elem()
Expand Down
12 changes: 12 additions & 0 deletions tm2/pkg/amino/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ func constructConcreteType(cinfo *TypeInfo) (crv, irvSet reflect.Value) {
return
}

func typeDescription(rv reflect.Value) (rt reflect.Type) {
var twrm reflect.Value
if rv.CanAddr() {
twrm = rv.Addr().MethodByName("TypeDesc")
} else {
twrm = rv.MethodByName("TypeDesc")
}

uwouts := twrm.Call([]reflect.Value{})
return uwouts[0].Interface().(reflect.Type)
}

func toReprObject(rv reflect.Value) (rrv reflect.Value, err error) {
var mwrm reflect.Value
if rv.CanAddr() {
Expand Down

0 comments on commit 1aebe75

Please sign in to comment.