Skip to content

Commit

Permalink
- java; lower camel case method and variable names in output, change …
Browse files Browse the repository at this point in the history
…output directory, fix output extension

- refactor template function
  • Loading branch information
ixje committed Sep 2, 2022
1 parent 049ba01 commit 3a36698
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
16 changes: 9 additions & 7 deletions generators/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (

type (
GenerateCfg struct {
Manifest *manifest.Manifest
ContractHash util.Uint160
ContractOutput *os.File
Manifest *manifest.Manifest
ContractHash util.Uint160
ContractOutput *os.File
ParamTypeConverter convertParam
MethodNameConverter func(s string) string
}

contractTmpl struct {
Expand All @@ -42,7 +44,7 @@ type (
convertParam func(typ smartcontract.ParamType) string
)

func templateFromManifest(cfg *GenerateCfg, scTypeToLang convertParam) (contractTmpl, error) {
func templateFromManifest(cfg *GenerateCfg) (contractTmpl, error) {
ctr := contractTmpl{
ContractName: upperFirst(cfg.Manifest.Name),
Hash: "0x" + cfg.ContractHash.StringLE(),
Expand All @@ -68,7 +70,7 @@ func templateFromManifest(cfg *GenerateCfg, scTypeToLang convertParam) (contract
seen[name] = true

mtd := methodTmpl{
Name: name,
Name: cfg.MethodNameConverter(name),
NameABI: method.Name,
Comment: fmt.Sprintf("invokes `%s` method of contract.", method.Name),
}
Expand All @@ -79,14 +81,14 @@ func templateFromManifest(cfg *GenerateCfg, scTypeToLang convertParam) (contract
name = fmt.Sprintf("arg%d", i)
}

var typeStr = scTypeToLang(method.Parameters[i].Type)
var typeStr = cfg.ParamTypeConverter(method.Parameters[i].Type)

mtd.Arguments = append(mtd.Arguments, paramTmpl{
Name: name,
Type: typeStr,
})
}
mtd.ReturnType = scTypeToLang(method.ReturnType)
mtd.ReturnType = cfg.ParamTypeConverter(method.ReturnType)
ctr.Methods = append(ctr.Methods, mtd)
}
return ctr, nil
Expand Down
5 changes: 4 additions & 1 deletion generators/csharp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generators

import (
"fmt"
"github.com/iancoleman/strcase"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
log "github.com/sirupsen/logrus"
"os"
Expand Down Expand Up @@ -44,7 +45,9 @@ func GenerateCsharpSDK(cfg *GenerateCfg) error {
return err
}

ctr, err := templateFromManifest(cfg, scTypeToCsharp)
cfg.MethodNameConverter = strcase.ToCamel
cfg.ParamTypeConverter = scTypeToCsharp
ctr, err := templateFromManifest(cfg)
if err != nil {
return err
}
Expand Down
17 changes: 10 additions & 7 deletions generators/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generators

import (
"fmt"
"github.com/iancoleman/strcase"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
log "github.com/sirupsen/logrus"
"os"
Expand All @@ -16,18 +17,18 @@ const javaSrcTmpl = `
{{- .Type}} {{.Name}}
{{- end}});
{{- end -}}
package io.coz.cpm
package <REPLACE_ME>
import io.neow3j.devpack.*;
import io.neow3j.devpack.contracts.ContractInterface;
public class {{ .ContractName }} extends ContractInterface {
static final String {{ .ContractName }}ScriptHash = "{{.Hash}}";
static final String scriptHash = "{{.Hash}}";
public {{ .ContractName }}() {
super({{ .ContractName }}ScriptHash);
super(scriptHash);
}
{{- range $m := .Methods}}
Expand All @@ -43,7 +44,9 @@ func GenerateJavaSDK(cfg *GenerateCfg) error {
return err
}

ctr, err := templateFromManifest(cfg, scTypeToJava)
cfg.MethodNameConverter = strcase.ToLowerCamel
cfg.ParamTypeConverter = scTypeToJava
ctr, err := templateFromManifest(cfg)
if err != nil {
return err
}
Expand All @@ -64,19 +67,19 @@ func GenerateJavaSDK(cfg *GenerateCfg) error {
return err
}

log.Infof("Created SDK for contract '%s' at %s/java/io/coz/cpm/ with contract hash 0x%s", cfg.Manifest.Name, wd, cfg.ContractHash.StringLE())
log.Infof("Created SDK for contract '%s' at %s/java/ with contract hash 0x%s", cfg.Manifest.Name, wd, cfg.ContractHash.StringLE())

return nil
}

func createJavaPackage(cfg *GenerateCfg) error {
err := os.MkdirAll("java/io/coz/cpm/", 0755)
err := os.Mkdir("java", 0755)
if err != nil {
return fmt.Errorf("can't create directory %s: %w", cfg.Manifest.Name, err)
}

filename := upperFirst(cfg.Manifest.Name)
f, err := os.Create(fmt.Sprintf("java/io/coz/cpm/%s.cs", filename))
f, err := os.Create(fmt.Sprintf("java/%s.java", filename))
if err != nil {
f.Close()
return fmt.Errorf("can't create %s.java file: %w", filename, err)
Expand Down
5 changes: 4 additions & 1 deletion generators/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generators

import (
"fmt"
"github.com/iancoleman/strcase"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
log "github.com/sirupsen/logrus"
"os"
Expand Down Expand Up @@ -49,7 +50,9 @@ func GeneratePythonSDK(cfg *GenerateCfg) error {
return err
}

ctr, err := templateFromManifest(cfg, scTypeToPython)
cfg.MethodNameConverter = strcase.ToSnake
cfg.ParamTypeConverter = scTypeToPython
ctr, err := templateFromManifest(cfg)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/google/uuid v1.2.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22 // indirect
github.com/nspcc-dev/rfc6979 v0.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
Expand Down

0 comments on commit 3a36698

Please sign in to comment.