Skip to content

Commit

Permalink
Merge pull request #2 from maxifom/add_all_mappings_ts
Browse files Browse the repository at this point in the history
add correct type mapping to typescript
  • Loading branch information
maxifom authored Mar 24, 2022
2 parents 1f3371f + 26b0fb3 commit 660aff5
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 45 deletions.
105 changes: 105 additions & 0 deletions examples/get_table_rows_all_types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
[
{
"user": "alltypesproj",
// name
"b": 1,
// bool
"i8": 127,
// int8
"ui8": 255,
// uint8
"i16": 32767,
// int16
"ui16": 65535,
// uint16
"i32": 2147483647,
// int32
"ui32": 4294967295,
// uint32
"vi32": 123123,
// varint32
"vui32": 123123,
// varuint32
"i64": "9223372036854775807",
// int64
"ui64": "18446744073709551615",
// uint64
"i128": "170141183460469231731687303715884105727",
// int128
"u128": "340282366920938463463374607431768211455",
// uint128
"f32": "1123123.12500000000000000",
// float32
"f64": "123312312.31231200695037842",
// float64
"f128": "0x401677c4f83f0b2c72be7d428fb65ab4",
// float128
"tp": "2001-01-01T00:00:00.000",
// time_point
"tps": "2001-01-01T00:00:00",
// time_point_sec
"btt": "2001-01-01T00:00:00.000",
// block_timestamp_type
"n": "alltypesproj",
// name
"byt": "aaaaaa",
// bytes
"s": "asdljkasdl",
// string
"c160": "ffffffffffffffffffffffffffffffffffffffff",
// checksum160
"c256": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
// checksum256
"c512": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
// checksum512
"pk": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
// public key
"symb": "8,WAX",
// symbol
"symb_code": "WAX",
// symbol code
"a": "1.00000000 WAX",
// asset
"ea": {
// extended_asset
"quantity": "1.00000000 WAX",
"contract": "eosio.token"
}
},
{
user: 'alltypesproj',
b: 1,
i8: 127,
ui8: 255,
i16: 32767,
ui16: 65535,
i32: 2147483647,
ui32: 4294967295,
i64: 1,
ui64: 1,
i128: '1',
u128: '1',
f32: '1.00000000000000000',
f64: '1.00000000000000000',
f128: '0x401677c4f83f0b2c72be7d428fb65ab4',
tp: '2001-01-01T00:00:00.000',
tps: '2001-01-01T00:00:00',
btt: '2001-01-01T00:00:00.000',
n: 'alltypesproj',
byt: 'aaaaaa',
s: 'asdljkasdl',
c160: 'ffffffffffffffffffffffffffffffffffffffff',
c256: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
c512: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
pk: 'EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV',
symb: '8,WAX',
symb_code: 'WAX',
a: '1.00000000 WAX',
ea: {
quantity: '1.00000000 WAX',
contract: 'eosio.token'
}
}
]


159 changes: 119 additions & 40 deletions pkg/commands/generate-ts/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ func Run(opts Opts) error {
return gen(abi, contractName, realContractName, opts.GeneratedFolder, opts.Version, t)
}

type Struct struct {
Name string
Fields []Field
}

type Method struct {
MethodName string
TableName string
ReturnName string
Struct Struct
}

func gen(abi abitypes.ABI, contractName string, realContractName string, generatedFolder string, version string, t *template.Template) error {
Expand Down Expand Up @@ -104,23 +110,6 @@ func gen(abi abitypes.ABI, contractName string, realContractName string, generat
return err
}

var methods []Method
for _, t := range abi.Tables {
methods = append(methods, Method{
MethodName: strings.ReplaceAll(strcase.LowerCamelCase(t.Name), ".", ""),
TableName: t.Name,
ReturnName: strcase.UpperCamelCase(t.Type) + "Rows",
})
}

err = t.ExecuteTemplate(clientF, "client", map[string]interface{}{
"Version": version,
"Methods": methods,
})
if err != nil {
return err
}

var rowTypes strings.Builder
var structTypes strings.Builder
declaredRowsNames := map[string]struct{}{}
Expand All @@ -139,7 +128,13 @@ func gen(abi abitypes.ABI, contractName string, realContractName string, generat
}

ss := genStructs(abi, getNewTypesMap(abi), getNewStructsMap(abi))
structMap := map[string]Struct{}
for _, s := range ss {
structMap[s.Name] = Struct{
Name: s.Name,
Fields: s.Fields,
}

err = t.ExecuteTemplate(&structTypes, "struct", map[string]interface{}{
"Name": s.Name,
"Fields": s.Fields,
Expand All @@ -156,6 +151,24 @@ func gen(abi abitypes.ABI, contractName string, realContractName string, generat
return err
}

var methods []Method
for _, t := range abi.Tables {
methods = append(methods, Method{
MethodName: strings.ReplaceAll(strcase.LowerCamelCase(t.Name), ".", ""),
TableName: t.Name,
ReturnName: strcase.UpperCamelCase(t.Type) + "Rows",
Struct: structMap[strcase.UpperCamelCase(t.Type)],
})
}

err = t.ExecuteTemplate(clientF, "client", map[string]interface{}{
"Version": version,
"Methods": methods,
})
if err != nil {
return err
}

return nil
}

Expand All @@ -178,15 +191,21 @@ func getNewStructsMap(abi abitypes.ABI) map[string]string {
}

type LanguageFieldMapping struct {
Type string
Type string
Func string
Method string
IntermediateType string
FullType string
}

var DefaultTSCaseFunc = strcase.UpperCamelCase

var LanguageMapping = map[string]map[string]LanguageFieldMapping{
"bool": {
"ts": {
Type: "boolean",
Type: "boolean",
IntermediateType: "number",
Func: "!!",
},
},
"int8": {
Expand Down Expand Up @@ -231,37 +250,43 @@ var LanguageMapping = map[string]map[string]LanguageFieldMapping{
},
"int64": {
"ts": {
Type: "number",
Type: "string",
Method: "toString",
},
},
"uint64": {
"ts": {
Type: "number",
Type: "string",
Method: "toString",
},
},
"int128": {
"ts": {
Type: "number",
Type: "string",
},
},
"uint128": {
"ts": {
Type: "number",
Type: "string",
},
},
"float32": {
"ts": {
Type: "number",
Type: "number",
Func: "Number.parseFloat",
IntermediateType: "string",
},
},
"float64": {
"ts": {
Type: "number",
Type: "number",
Func: "Number.parseFloat",
IntermediateType: "string",
},
},
"float128": {
"ts": {
Type: "number",
Type: "string",
},
},
"time_point": {
Expand Down Expand Up @@ -321,7 +346,10 @@ var LanguageMapping = map[string]map[string]LanguageFieldMapping{
},
"symbol": {
"ts": {
Type: "string",
Type: "Symbol",
Func: "new types.Symbol",
FullType: "types.Symbol",
IntermediateType: "string",
},
},
"symbol_code": {
Expand All @@ -331,19 +359,31 @@ var LanguageMapping = map[string]map[string]LanguageFieldMapping{
},
"asset": {
"ts": {
Type: "string",
Type: "Asset",
Func: "new types.Asset",
IntermediateType: "string",
FullType: "types.Asset",
},
},
"extended_asset": {
"ts": {
Type: "string",
Type: "ExtendedAsset",
Func: "new types.ExtendedAsset",
IntermediateType: "ExtendedAssetType",
FullType: "types.ExtendedAsset",
},
},
}

type Field struct {
Name string
Type string
Name string
Type string
FullType string
IntermediateType string
Func string
Method string
ArraysCount int64
ArraysCountIterator []int
}

type S struct {
Expand All @@ -361,29 +401,68 @@ func genStructs(abi abitypes.ABI, newTypesMap map[string]string, newStructsMap m
for _, field := range abiStruct.Fields {
fieldName := strings.ToLower(field.Name)
fieldType := field.Type
listsSuffix := strings.Repeat("[]", strings.Count(fieldType, "[]"))
arraysCount := strings.Count(fieldType, "[]")
var aci []int
for i := 0; i < arraysCount; i++ {
aci = append(aci, i)
}
listsSuffix := strings.Repeat("[]", arraysCount)
fieldType = strings.ReplaceAll(fieldType, "[]", "")
if realFieldType, ok := newTypesMap[fieldType]; ok {
fieldType = realFieldType
}

if fMapping, ok := LanguageMapping[fieldType]["ts"]; ok {
realType := fMapping.Type + listsSuffix
intermediateType := ""
if fMapping.IntermediateType != "" {
intermediateType = fMapping.IntermediateType + listsSuffix
}

s.Fields = append(s.Fields, Field{
Name: fieldName,
Type: realType,
Name: fieldName,
Type: realType,
IntermediateType: intermediateType,
Func: fMapping.Func,
Method: fMapping.Method,
ArraysCount: int64(arraysCount),
ArraysCountIterator: aci,
FullType: fMapping.FullType + listsSuffix,
})
} else {
if structName, ok := newStructsMap[fieldType]; ok {
realType := structName + listsSuffix
s.Fields = append(s.Fields, Field{
Name: fieldName,
Type: realType,
})

if fmapping, ok := LanguageMapping[structName]["ts"]; ok {
intermediateType := ""
if fMapping.IntermediateType != "" {
intermediateType = fMapping.IntermediateType + listsSuffix
}
s.Fields = append(s.Fields, Field{
Name: fieldName,
Type: realType,
IntermediateType: intermediateType,
Func: fmapping.Func,
Method: fMapping.Method,
ArraysCount: int64(arraysCount),
ArraysCountIterator: aci,
FullType: fMapping.FullType + listsSuffix,
})
} else {
s.Fields = append(s.Fields, Field{
Name: fieldName,
Type: realType,
ArraysCount: int64(arraysCount),
ArraysCountIterator: aci,
})
}

} else {
s.Fields = append(s.Fields, Field{
Name: fieldName,
Type: "unknown",
Name: fieldName,
Type: "unknown" + listsSuffix,
ArraysCount: int64(arraysCount),
ArraysCountIterator: aci,
})
}
}
Expand Down
Loading

0 comments on commit 660aff5

Please sign in to comment.