binding: avoid name conflicts with Go keywords

And clashing one name on another after rename.
This commit is contained in:
Roman Khimov 2022-10-27 22:35:06 +03:00
parent 3b635164b7
commit 02ce59cfd5
3 changed files with 28 additions and 5 deletions

View file

@ -45,6 +45,15 @@ func TestGenerate(t *testing.T) {
ReturnType: smartcontract.IntegerType,
Safe: true,
},
manifest.Method{
Name: "zum",
Parameters: []manifest.Parameter{
manifest.NewParameter("type", smartcontract.IntegerType),
manifest.NewParameter("typev", smartcontract.IntegerType),
manifest.NewParameter("func", smartcontract.IntegerType),
},
ReturnType: smartcontract.IntegerType,
},
manifest.Method{
Name: "justExecute",
Parameters: []manifest.Parameter{
@ -172,6 +181,11 @@ func Sum3() int {
return neogointernal.CallWithToken(Hash, "sum3", int(contract.ReadOnly)).(int)
}
// Zum invokes ` + "`zum`" + ` method of contract.
func Zum(typev int, typev_ int, funcv int) int {
return neogointernal.CallWithToken(Hash, "zum", int(contract.All), typev, typev_, funcv).(int)
}
// JustExecute invokes ` + "`justExecute`" + ` method of contract.
func JustExecute(arr []interface{}) {
neogointernal.CallWithTokenNoRet(Hash, "justExecute", int(contract.All), arr)

View file

@ -47,8 +47,8 @@ func (c *ContractReader) IsAvailable(name string) (bool, error) {
}
// GetRecord invokes `getRecord` method of contract.
func (c *ContractReader) GetRecord(name string, type *big.Int) (string, error) {
return unwrap.UTF8String(c.invoker.Call(Hash, "getRecord", name, type))
func (c *ContractReader) GetRecord(name string, typev *big.Int) (string, error) {
return unwrap.UTF8String(c.invoker.Call(Hash, "getRecord", name, typev))
}
// GetAllRecords invokes `getAllRecords` method of contract.
@ -57,6 +57,6 @@ func (c *ContractReader) GetAllRecords(name string) (stackitem.Item, error) {
}
// Resolve invokes `resolve` method of contract.
func (c *ContractReader) Resolve(name string, type *big.Int) (string, error) {
return unwrap.UTF8String(c.invoker.Call(Hash, "resolve", name, type))
func (c *ContractReader) Resolve(name string, typev *big.Int) (string, error) {
return unwrap.UTF8String(c.invoker.Call(Hash, "resolve", name, typev))
}

View file

@ -3,6 +3,7 @@ package binding
import (
"bytes"
"fmt"
"go/token"
"io"
"sort"
"strconv"
@ -201,6 +202,8 @@ func TemplateFromManifest(cfg Config, scTypeConverter func(string, smartcontract
} else if m.Safe {
mtd.CallFlag = callflag.ReadOnly.String()
}
var varnames = make(map[string]bool)
for i := range m.Parameters {
name := m.Parameters[i].Name
if name == "" {
@ -211,7 +214,13 @@ func TemplateFromManifest(cfg Config, scTypeConverter func(string, smartcontract
if pkg != "" {
imports[pkg] = struct{}{}
}
if token.IsKeyword(name) {
name = name + "v"
}
for varnames[name] {
name = name + "_"
}
varnames[name] = true
mtd.Arguments = append(mtd.Arguments, ParamTmpl{
Name: name,
Type: typeStr,