github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/amino/tests/common.go (about)

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"time"
     7  )
     8  
     9  // ----------------------------------------
    10  // Struct types
    11  
    12  type EmptyStruct struct{}
    13  
    14  type PrimitivesStruct struct {
    15  	Int8        int8
    16  	Int16       int16
    17  	Int32       int32
    18  	Int32Fixed  int32 `binary:"fixed32"`
    19  	Int64       int64
    20  	Int64Fixed  int64 `binary:"fixed64"`
    21  	Int         int
    22  	Byte        byte
    23  	Uint8       uint8
    24  	Uint16      uint16
    25  	Uint32      uint32
    26  	Uint32Fixed uint32 `binary:"fixed32"`
    27  	Uint64      uint64
    28  	Uint64Fixed uint64 `binary:"fixed64"`
    29  	Uint        uint
    30  	Str         string
    31  	Bytes       []byte
    32  	Time        time.Time
    33  	Duration    time.Duration
    34  	Empty       EmptyStruct
    35  }
    36  
    37  type ShortArraysStruct struct {
    38  	TimeAr     [0]time.Time
    39  	DurationAr [0]time.Duration
    40  }
    41  
    42  type ArraysStruct struct {
    43  	Int8Ar        [4]int8
    44  	Int16Ar       [4]int16
    45  	Int32Ar       [4]int32
    46  	Int32FixedAr  [4]int32 `binary:"fixed32"`
    47  	Int64Ar       [4]int64
    48  	Int64FixedAr  [4]int64 `binary:"fixed64"`
    49  	IntAr         [4]int
    50  	ByteAr        [4]byte
    51  	Uint8Ar       [4]uint8
    52  	Uint16Ar      [4]uint16
    53  	Uint32Ar      [4]uint32
    54  	Uint32FixedAr [4]uint32 `binary:"fixed32"`
    55  	Uint64Ar      [4]uint64
    56  	Uint64FixedAr [4]uint64 `binary:"fixed64"`
    57  	UintAr        [4]uint
    58  	StrAr         [4]string
    59  	BytesAr       [4][]byte
    60  	TimeAr        [4]time.Time
    61  	DurationAr    [4]time.Duration
    62  	EmptyAr       [4]EmptyStruct
    63  }
    64  
    65  type ArraysArraysStruct struct {
    66  	Int8ArAr        [2][2]int8
    67  	Int16ArAr       [2][2]int16
    68  	Int32ArAr       [2][2]int32
    69  	Int32FixedArAr  [2][2]int32 `binary:"fixed32"`
    70  	Int64ArAr       [2][2]int64
    71  	Int64FixedArAr  [2][2]int64 `binary:"fixed64"`
    72  	IntArAr         [2][2]int
    73  	ByteArAr        [2][2]byte
    74  	Uint8ArAr       [2][2]uint8
    75  	Uint16ArAr      [2][2]uint16
    76  	Uint32ArAr      [2][2]uint32
    77  	Uint32FixedArAr [2][2]uint32 `binary:"fixed32"`
    78  	Uint64ArAr      [2][2]uint64
    79  	Uint64FixedArAr [2][2]uint64 `binary:"fixed64"`
    80  	UintArAr        [2][2]uint
    81  	StrArAr         [2][2]string
    82  	BytesArAr       [2][2][]byte
    83  	TimeArAr        [2][2]time.Time
    84  	DurationArAr    [2][2]time.Duration
    85  	EmptyArAr       [2][2]EmptyStruct
    86  }
    87  
    88  type SlicesStruct struct {
    89  	Int8Sl        []int8
    90  	Int16Sl       []int16
    91  	Int32Sl       []int32
    92  	Int32FixedSl  []int32 `binary:"fixed32"`
    93  	Int64Sl       []int64
    94  	Int64FixedSl  []int64 `binary:"fixed64"`
    95  	IntSl         []int
    96  	ByteSl        []byte
    97  	Uint8Sl       []uint8
    98  	Uint16Sl      []uint16
    99  	Uint32Sl      []uint32
   100  	Uint32FixedSl []uint32 `binary:"fixed32"`
   101  	Uint64Sl      []uint64
   102  	Uint64FixedSl []uint64 `binary:"fixed64"`
   103  	UintSl        []uint
   104  	StrSl         []string
   105  	BytesSl       [][]byte
   106  	TimeSl        []time.Time
   107  	DurationSl    []time.Duration
   108  	EmptySl       []EmptyStruct
   109  }
   110  
   111  type SlicesSlicesStruct struct {
   112  	Int8SlSl        [][]int8
   113  	Int16SlSl       [][]int16
   114  	Int32SlSl       [][]int32
   115  	Int32FixedSlSl  [][]int32 `binary:"fixed32"`
   116  	Int64SlSl       [][]int64
   117  	Int64FixedSlSl  [][]int64 `binary:"fixed64"`
   118  	IntSlSl         [][]int
   119  	ByteSlSl        [][]byte
   120  	Uint8SlSl       [][]uint8
   121  	Uint16SlSl      [][]uint16
   122  	Uint32SlSl      [][]uint32
   123  	Uint32FixedSlSl [][]uint32 `binary:"fixed32"`
   124  	Uint64SlSl      [][]uint64
   125  	Uint64FixedSlSl [][]uint64 `binary:"fixed64"`
   126  	UintSlSl        [][]uint
   127  	StrSlSl         [][]string
   128  	BytesSlSl       [][][]byte
   129  	TimeSlSl        [][]time.Time
   130  	DurationSlSl    [][]time.Duration
   131  	EmptySlSl       [][]EmptyStruct
   132  }
   133  
   134  type PointersStruct struct {
   135  	Int8Pt        *int8
   136  	Int16Pt       *int16
   137  	Int32Pt       *int32
   138  	Int32FixedPt  *int32 `binary:"fixed32"`
   139  	Int64Pt       *int64
   140  	Int64FixedPt  *int64 `binary:"fixed64"`
   141  	IntPt         *int
   142  	BytePt        *byte
   143  	Uint8Pt       *uint8
   144  	Uint16Pt      *uint16
   145  	Uint32Pt      *uint32
   146  	Uint32FixedPt *uint32 `binary:"fixed32"`
   147  	Uint64Pt      *uint64
   148  	Uint64FixedPt *uint64 `binary:"fixed64"`
   149  	UintPt        *uint
   150  	StrPt         *string
   151  	BytesPt       *[]byte
   152  	TimePt        *time.Time
   153  	DurationPt    *time.Duration
   154  	EmptyPt       *EmptyStruct
   155  }
   156  
   157  type PointerSlicesStruct struct {
   158  	Int8PtSl        []*int8
   159  	Int16PtSl       []*int16
   160  	Int32PtSl       []*int32
   161  	Int32FixedPtSl  []*int32 `binary:"fixed32"`
   162  	Int64PtSl       []*int64
   163  	Int64FixedPtSl  []*int64 `binary:"fixed64"`
   164  	IntPtSl         []*int
   165  	BytePtSl        []*byte
   166  	Uint8PtSl       []*uint8
   167  	Uint16PtSl      []*uint16
   168  	Uint32PtSl      []*uint32
   169  	Uint32FixedPtSl []*uint32 `binary:"fixed32"`
   170  	Uint64PtSl      []*uint64
   171  	Uint64FixedPtSl []*uint64 `binary:"fixed64"`
   172  	UintPtSl        []*uint
   173  	StrPtSl         []*string
   174  	BytesPtSl       []*[]byte
   175  	TimePtSl        []*time.Time
   176  	DurationPtSl    []*time.Duration
   177  	EmptyPtSl       []*EmptyStruct
   178  }
   179  
   180  /* amino 1.2 removed nested pointer support.
   181  // NOTE: See registered fuzz funcs for *byte, **byte, and ***byte.
   182  type NestedPointersStruct struct {
   183  	Ptr1 *byte
   184  	Ptr2 **byte
   185  	Ptr3 ***byte
   186  }
   187  */
   188  
   189  // ----------------------------------------
   190  // AminoMarshalerStruct1
   191  // struct -> repr struct
   192  
   193  type AminoMarshalerStruct1 struct {
   194  	A int32
   195  	B int32
   196  }
   197  
   198  type ReprStruct1 struct {
   199  	C int64
   200  	D int64
   201  }
   202  
   203  func (ams AminoMarshalerStruct1) MarshalAmino() (ReprStruct1, error) {
   204  	return ReprStruct1{
   205  		C: int64(ams.A),
   206  		D: int64(ams.B),
   207  	}, nil
   208  }
   209  
   210  func (ams *AminoMarshalerStruct1) UnmarshalAmino(rs ReprStruct1) error {
   211  	ams.A = int32(rs.C)
   212  	ams.B = int32(rs.D)
   213  	return nil
   214  }
   215  
   216  // ----------------------------------------
   217  // AminoMarshalerStruct2
   218  // struct -> []struct
   219  
   220  type AminoMarshalerStruct2 struct {
   221  	a string // unexposed (dontcare)
   222  	B int32  // exposed (dontcare)
   223  }
   224  
   225  type ReprElem2 struct {
   226  	Key   string
   227  	Value interface{}
   228  }
   229  
   230  func (re ReprElem2) get(key string) (value interface{}) {
   231  	if re.Key != key {
   232  		panic(fmt.Sprintf("wanted %v but is %v", key, re.Key))
   233  	}
   234  	return re.Value
   235  }
   236  
   237  func (ams AminoMarshalerStruct2) MarshalAmino() ([]ReprElem2, error) {
   238  	return []ReprElem2{
   239  		{"a", ams.a},
   240  		{"B", ams.B},
   241  	}, nil
   242  }
   243  
   244  func (ams *AminoMarshalerStruct2) UnmarshalAmino(repr []ReprElem2) error {
   245  	ams.a = repr[0].get("a").(string)
   246  	ams.B = repr[1].get("B").(int32)
   247  	return nil
   248  }
   249  
   250  // ----------------------------------------
   251  // AminoMarshalerStruct3
   252  // struct -> int
   253  
   254  type AminoMarshalerStruct3 struct {
   255  	A int32
   256  }
   257  
   258  func (ams AminoMarshalerStruct3) MarshalAmino() (int32, error) {
   259  	return ams.A, nil
   260  }
   261  
   262  func (ams *AminoMarshalerStruct3) UnmarshalAmino(i int32) error {
   263  	ams.A = i
   264  	return nil
   265  }
   266  
   267  // ----------------------------------------
   268  // AminoMarshalerInt4
   269  // int -> struct
   270  
   271  type AminoMarshalerInt4 int32
   272  
   273  type ReprStruct4 struct {
   274  	A int32
   275  }
   276  
   277  func (am AminoMarshalerInt4) MarshalAmino() (ReprStruct4, error) {
   278  	return ReprStruct4{A: int32(am)}, nil
   279  }
   280  
   281  func (am *AminoMarshalerInt4) UnmarshalAmino(rs ReprStruct4) error {
   282  	*am = AminoMarshalerInt4(rs.A)
   283  	return nil
   284  }
   285  
   286  // ----------------------------------------
   287  // AminoMarshalerInt5
   288  // int -> string
   289  
   290  type AminoMarshalerInt5 int32
   291  
   292  func (am AminoMarshalerInt5) MarshalAmino() (string, error) {
   293  	return fmt.Sprintf("%v", am), nil
   294  }
   295  
   296  func (am *AminoMarshalerInt5) UnmarshalAmino(repr string) error {
   297  	i, err := strconv.Atoi(repr)
   298  	if err != nil {
   299  		return err
   300  	}
   301  	*am = AminoMarshalerInt5(i)
   302  	return nil
   303  }
   304  
   305  // ----------------------------------------
   306  // AminoMarshalerStruct6
   307  // struct -> []struct, where elems are struct -> struct
   308  
   309  type AminoMarshalerStruct6 struct {
   310  	A int32
   311  	B int32
   312  }
   313  
   314  func (ams AminoMarshalerStruct6) MarshalAmino() ([]AminoMarshalerStruct1, error) {
   315  	return []AminoMarshalerStruct1{{A: ams.A, B: ams.B}}, nil
   316  }
   317  
   318  func (ams *AminoMarshalerStruct6) UnmarshalAmino(repr []AminoMarshalerStruct1) error {
   319  	ams.A = repr[0].A
   320  	ams.B = repr[0].B
   321  	return nil
   322  }
   323  
   324  // ----------------------------------------
   325  // AminoMarshalerStruct7
   326  // struct -> []struct, where elems are struct -> byte
   327  // NOTE: this should optimize to p3 bytes.
   328  
   329  type AminoMarshalerStruct7 struct {
   330  	A int8
   331  }
   332  
   333  func (ams AminoMarshalerStruct7) MarshalAmino() ([]ReprElem7, error) {
   334  	return []ReprElem7{{A: ams.A}}, nil
   335  }
   336  
   337  func (ams *AminoMarshalerStruct7) UnmarshalAmino(repr []ReprElem7) error {
   338  	ams.A = repr[0].A
   339  	return nil
   340  }
   341  
   342  type ReprElem7 struct {
   343  	A int8
   344  }
   345  
   346  func (re ReprElem7) MarshalAmino() (uint8, error) {
   347  	return uint8(re.A), nil
   348  }
   349  
   350  func (re *ReprElem7) UnmarshalAmino(u uint8) error {
   351  	re.A = int8(u)
   352  	return nil
   353  }
   354  
   355  // ----------------------------------------
   356  
   357  type ComplexSt struct {
   358  	PrField PrimitivesStruct
   359  	ArField ArraysStruct
   360  	SlField SlicesStruct
   361  	PtField PointersStruct
   362  }
   363  
   364  type EmbeddedSt1 struct {
   365  	PrimitivesStruct
   366  }
   367  
   368  type EmbeddedSt2 struct {
   369  	PrimitivesStruct
   370  	ArraysStruct
   371  	SlicesStruct
   372  	PointersStruct
   373  }
   374  
   375  type EmbeddedSt3 struct {
   376  	*PrimitivesStruct
   377  	*ArraysStruct
   378  	*SlicesStruct
   379  	*PointersStruct
   380  	*EmptyStruct
   381  }
   382  
   383  type EmbeddedSt4 struct {
   384  	Foo1 int
   385  	PrimitivesStruct
   386  	Foo2              string
   387  	ArraysStructField ArraysStruct
   388  	Foo3              []byte
   389  	SlicesStruct
   390  	Foo4                bool
   391  	PointersStructField PointersStruct
   392  	Foo5                uint
   393  }
   394  
   395  type EmbeddedSt5 struct {
   396  	Foo1 int
   397  	*PrimitivesStruct
   398  	Foo2              string
   399  	ArraysStructField *ArraysStruct
   400  	Foo3              []byte
   401  	*SlicesStruct
   402  	Foo4                bool
   403  	PointersStructField *PointersStruct
   404  	Foo5                uint
   405  }
   406  
   407  var StructTypes = []interface{}{
   408  	(*EmptyStruct)(nil),
   409  	(*PrimitivesStruct)(nil),
   410  	(*ShortArraysStruct)(nil),
   411  	(*ArraysStruct)(nil),
   412  	(*ArraysArraysStruct)(nil),
   413  	(*SlicesStruct)(nil),
   414  	(*SlicesSlicesStruct)(nil),
   415  	(*PointersStruct)(nil),
   416  	(*PointerSlicesStruct)(nil),
   417  	// (*NestedPointersStruct)(nil),
   418  	(*ComplexSt)(nil),
   419  	(*EmbeddedSt1)(nil),
   420  	(*EmbeddedSt2)(nil),
   421  	(*EmbeddedSt3)(nil),
   422  	(*EmbeddedSt4)(nil),
   423  	(*EmbeddedSt5)(nil),
   424  	(*AminoMarshalerStruct1)(nil),
   425  	(*AminoMarshalerStruct2)(nil),
   426  	(*AminoMarshalerStruct3)(nil),
   427  	(*AminoMarshalerInt4)(nil),
   428  	(*AminoMarshalerInt5)(nil),
   429  	(*AminoMarshalerStruct6)(nil),
   430  	(*AminoMarshalerStruct7)(nil),
   431  }
   432  
   433  // ----------------------------------------
   434  // Type definition types
   435  
   436  // This will be encoded as
   437  // message SomeName { int64 val = 1; }
   438  type IntDef int
   439  
   440  // This will be encoded as
   441  // message SomeName { repeated int val = 1; }
   442  type IntAr [4]int
   443  
   444  // This will be encoded as
   445  // message SomeName { repeated int val = 1; }
   446  type IntSl []int
   447  
   448  // This will be encoded as
   449  // message SomeName { bytes val = 1; }
   450  type ByteAr [4]byte
   451  
   452  // This will be encoded as
   453  // message SomeName { bytes val = 1; }
   454  type ByteSl []byte
   455  
   456  type PrimitivesStructDef PrimitivesStruct
   457  
   458  // This will be encoded as
   459  // message SomeName { repeated PrimitivesStruct val = 1; }
   460  type PrimitivesStructSl []PrimitivesStruct
   461  
   462  // This will be encoded as
   463  // message SomeName { repeated PrimitivesStruct val = 1; }
   464  type PrimitivesStructAr [2]PrimitivesStruct
   465  
   466  var DefTypes = []interface{}{
   467  	(*IntDef)(nil),
   468  	(*IntAr)(nil),
   469  	(*IntSl)(nil),
   470  	(*ByteAr)(nil),
   471  	(*ByteSl)(nil),
   472  	(*PrimitivesStructSl)(nil),
   473  	(*PrimitivesStructDef)(nil),
   474  }
   475  
   476  // ----------------------------------------
   477  // Register/Interface test types
   478  
   479  type Interface1 interface {
   480  	AssertInterface1()
   481  }
   482  
   483  type Interface2 interface {
   484  	AssertInterface2()
   485  }
   486  
   487  type Concrete1 struct{}
   488  
   489  func (Concrete1) AssertInterface1() {}
   490  func (Concrete1) AssertInterface2() {}
   491  
   492  type Concrete2 struct{}
   493  
   494  func (Concrete2) AssertInterface1() {}
   495  func (Concrete2) AssertInterface2() {}
   496  
   497  // Special case: this concrete implementation (of Interface1) is a type definition.
   498  type ConcreteTypeDef [4]byte
   499  
   500  func (ConcreteTypeDef) AssertInterface1() {}
   501  
   502  // Ideally, user's of amino should refrain from using the above
   503  // but wrap actual values in structs; e.g. like:
   504  type ConcreteWrappedBytes struct {
   505  	Value []byte
   506  }
   507  
   508  func (ConcreteWrappedBytes) AssertInterface1() {}
   509  
   510  // Yet another special case: Field could be a type alias (should not be wrapped).
   511  type InterfaceFieldsStruct struct {
   512  	F1 Interface1
   513  	F2 Interface1
   514  	F3 interface{}
   515  	F4 interface{}
   516  }
   517  
   518  func (*InterfaceFieldsStruct) AssertInterface1() {}