rpcbinding: fix binding indentation

New rule for writing blocks of code to our template: new line before
the block starts and new line after the block ends. This rule is the
same as the one we use during manual typing.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2023-05-24 11:44:31 +03:00
parent 0f0f7b364f
commit ae52b2c2fa
9 changed files with 42 additions and 62 deletions

View file

@ -316,7 +316,6 @@ func NewReader(invoker Invoker) *ContractReader {
return &ContractReader{invoker} return &ContractReader{invoker}
} }
// Get invokes `+"`get`"+` method of contract. // Get invokes `+"`get`"+` method of contract.
func (c *ContractReader) Get() (*big.Int, error) { func (c *ContractReader) Get() (*big.Int, error) {
return unwrap.BigInt(c.invoker.Call(Hash, "get")) return unwrap.BigInt(c.invoker.Call(Hash, "get"))

View file

@ -44,4 +44,3 @@ func New(actor Actor) *Contract {
var nep17t = nep17.New(actor, Hash) var nep17t = nep17.New(actor, Hash)
return &Contract{ContractReader{nep17t.TokenReader, actor}, nep17t.TokenWriter, actor} return &Contract{ContractReader{nep17t.TokenReader, actor}, nep17t.TokenWriter, actor}
} }

View file

@ -59,7 +59,6 @@ func New(actor Actor) *Contract {
return &Contract{ContractReader{nep11ndt.NonDivisibleReader, actor}, nep11ndt.BaseWriter, actor} return &Contract{ContractReader{nep11ndt.NonDivisibleReader, actor}, nep11ndt.BaseWriter, actor}
} }
// Roots invokes `roots` method of contract. // Roots invokes `roots` method of contract.
func (c *ContractReader) Roots() (uuid.UUID, result.Iterator, error) { func (c *ContractReader) Roots() (uuid.UUID, result.Iterator, error) {
return unwrap.SessionIterator(c.invoker.Call(Hash, "roots")) return unwrap.SessionIterator(c.invoker.Call(Hash, "roots"))

View file

@ -56,7 +56,6 @@ func New(actor Actor) *Contract {
return &Contract{ContractReader{nep17t.TokenReader, actor}, nep17t.TokenWriter, actor} return &Contract{ContractReader{nep17t.TokenReader, actor}, nep17t.TokenWriter, actor}
} }
// Cap invokes `cap` method of contract. // Cap invokes `cap` method of contract.
func (c *ContractReader) Cap() (*big.Int, error) { func (c *ContractReader) Cap() (*big.Int, error) {
return unwrap.BigInt(c.invoker.Call(Hash, "cap")) return unwrap.BigInt(c.invoker.Call(Hash, "cap"))

View file

@ -30,7 +30,6 @@ func NewReader(invoker Invoker) *ContractReader {
return &ContractReader{invoker} return &ContractReader{invoker}
} }
// Tokens invokes `tokens` method of contract. // Tokens invokes `tokens` method of contract.
func (c *ContractReader) Tokens() (uuid.UUID, result.Iterator, error) { func (c *ContractReader) Tokens() (uuid.UUID, result.Iterator, error) {
return unwrap.SessionIterator(c.invoker.Call(Hash, "tokens")) return unwrap.SessionIterator(c.invoker.Call(Hash, "tokens"))

View file

@ -17,7 +17,6 @@ import (
// Hash contains contract hash. // Hash contains contract hash.
var Hash = util.Uint160{0x33, 0x22, 0x11, 0x0, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x0} var Hash = util.Uint160{0x33, 0x22, 0x11, 0x0, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x0}
// LedgerBlock is a contract-specific ledger.Block type used by its methods. // LedgerBlock is a contract-specific ledger.Block type used by its methods.
type LedgerBlock struct { type LedgerBlock struct {
Hash util.Uint256 Hash util.Uint256
@ -154,6 +153,7 @@ type StructsInternal struct {
Map map[*big.Int]keys.PublicKeys Map map[*big.Int]keys.PublicKeys
Struct *StructsInternal Struct *StructsInternal
} }
// Invoker is used by ContractReader to call various safe methods. // Invoker is used by ContractReader to call various safe methods.
type Invoker interface { type Invoker interface {
Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error) Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error)
@ -169,7 +169,6 @@ func NewReader(invoker Invoker) *ContractReader {
return &ContractReader{invoker} return &ContractReader{invoker}
} }
// Block invokes `block` method of contract. // Block invokes `block` method of contract.
func (c *ContractReader) Block(b *LedgerBlock) (*LedgerBlock, error) { func (c *ContractReader) Block(b *LedgerBlock) (*LedgerBlock, error) {
return itemToLedgerBlock(unwrap.Item(c.invoker.Call(Hash, "block", b))) return itemToLedgerBlock(unwrap.Item(c.invoker.Call(Hash, "block", b)))
@ -309,7 +308,6 @@ func (res *LedgerBlock) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field TransactionsLength: %w", err) return fmt.Errorf("field TransactionsLength: %w", err)
} }
return nil return nil
} }
@ -448,7 +446,6 @@ func (res *LedgerBlockSR) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field PrevStateRoot: %w", err) return fmt.Errorf("field PrevStateRoot: %w", err)
} }
return nil return nil
} }
@ -545,7 +542,6 @@ func (res *LedgerTransaction) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field Script: %w", err) return fmt.Errorf("field Script: %w", err)
} }
return nil return nil
} }
@ -673,7 +669,6 @@ func (res *LedgerTransactionSigner) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field Rules: %w", err) return fmt.Errorf("field Rules: %w", err)
} }
return nil return nil
} }
@ -714,7 +709,6 @@ func (res *LedgerWitnessCondition) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field Value: %w", err) return fmt.Errorf("field Value: %w", err)
} }
return nil return nil
} }
@ -755,7 +749,6 @@ func (res *LedgerWitnessRule) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field Condition: %w", err) return fmt.Errorf("field Condition: %w", err)
} }
return nil return nil
} }
@ -822,7 +815,6 @@ func (res *ManagementABI) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field Events: %w", err) return fmt.Errorf("field Events: %w", err)
} }
return nil return nil
} }
@ -891,7 +883,6 @@ func (res *ManagementContract) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field Manifest: %w", err) return fmt.Errorf("field Manifest: %w", err)
} }
return nil return nil
} }
@ -954,7 +945,6 @@ func (res *ManagementEvent) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field Params: %w", err) return fmt.Errorf("field Params: %w", err)
} }
return nil return nil
} }
@ -1005,7 +995,6 @@ func (res *ManagementGroup) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field Signature: %w", err) return fmt.Errorf("field Signature: %w", err)
} }
return nil return nil
} }
@ -1198,7 +1187,6 @@ func (res *ManagementManifest) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field Extra: %w", err) return fmt.Errorf("field Extra: %w", err)
} }
return nil return nil
} }
@ -1279,7 +1267,6 @@ func (res *ManagementMethod) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field Safe: %w", err) return fmt.Errorf("field Safe: %w", err)
} }
return nil return nil
} }
@ -1329,7 +1316,6 @@ func (res *ManagementParameter) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field Type: %w", err) return fmt.Errorf("field Type: %w", err)
} }
return nil return nil
} }
@ -1402,7 +1388,6 @@ func (res *ManagementPermission) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field Methods: %w", err) return fmt.Errorf("field Methods: %w", err)
} }
return nil return nil
} }
@ -1635,6 +1620,5 @@ func (res *StructsInternal) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field Struct: %w", err) return fmt.Errorf("field Struct: %w", err)
} }
return nil return nil
} }

View file

@ -31,7 +31,6 @@ func NewReader(invoker Invoker) *ContractReader {
return &ContractReader{invoker} return &ContractReader{invoker}
} }
// AAAStrings invokes `aAAStrings` method of contract. // AAAStrings invokes `aAAStrings` method of contract.
func (c *ContractReader) AAAStrings(s [][][]string) ([][][]string, error) { func (c *ContractReader) AAAStrings(s [][][]string) ([][][]string, error) {
return func (item stackitem.Item, err error) ([][][]string, error) { return func (item stackitem.Item, err error) ([][][]string, error) {

View file

@ -30,7 +30,6 @@ func New(actor Actor) *Contract {
return &Contract{actor} return &Contract{actor}
} }
func scriptForVerify() ([]byte, error) { func scriptForVerify() ([]byte, error) {
return smartcontract.CreateCallWithAssertScript(Hash, "verify") return smartcontract.CreateCallWithAssertScript(Hash, "verify")
} }

View file

@ -12,8 +12,12 @@ import (
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest/standard" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest/standard"
) )
const srcTmpl = ` // The set of constants containing parts of RPC binding template. Each block of code
{{- define "SAFEMETHOD" -}} // including template definition and var/type/method definitions contain new line at the
// start and ends with a new line. On adding new block of code to the template, please,
// ensure that this block has new line at the start and in the end of the block.
const (
safemethodDefinition = `{{ define "SAFEMETHOD" }}
// {{.Name}} {{.Comment}} // {{.Name}} {{.Comment}}
func (c *ContractReader) {{.Name}}({{range $index, $arg := .Arguments -}} func (c *ContractReader) {{.Name}}({{range $index, $arg := .Arguments -}}
{{- if ne $index 0}}, {{end}} {{- if ne $index 0}}, {{end}}
@ -32,8 +36,7 @@ func (c *ContractReader) {{.Name}}({{range $index, $arg := .Arguments -}}
{{- range $arg := .Arguments -}}, {{.Name}}{{end}}) {{- range $arg := .Arguments -}}, {{.Name}}{{end}})
{{- end}} {{- end}}
} }
{{- if eq .Unwrapper "SessionIterator"}} {{ if eq .Unwrapper "SessionIterator" }}
// {{.Name}}Expanded is similar to {{.Name}} (uses the same contract // {{.Name}}Expanded is similar to {{.Name}} (uses the same contract
// method), but can be useful if the server used doesn't support sessions and // method), but can be useful if the server used doesn't support sessions and
// doesn't expand iterators. It creates a script that will get the specified // doesn't expand iterators. It creates a script that will get the specified
@ -42,17 +45,16 @@ func (c *ContractReader) {{.Name}}({{range $index, $arg := .Arguments -}}
func (c *ContractReader) {{.Name}}Expanded({{range $index, $arg := .Arguments}}{{.Name}} {{.Type}}, {{end}}_numOfIteratorItems int) ([]stackitem.Item, error) { func (c *ContractReader) {{.Name}}Expanded({{range $index, $arg := .Arguments}}{{.Name}} {{.Type}}, {{end}}_numOfIteratorItems int) ([]stackitem.Item, error) {
return unwrap.Array(c.invoker.CallAndExpandIterator(Hash, "{{.NameABI}}", _numOfIteratorItems{{range $arg := .Arguments}}, {{.Name}}{{end}})) return unwrap.Array(c.invoker.CallAndExpandIterator(Hash, "{{.NameABI}}", _numOfIteratorItems{{range $arg := .Arguments}}, {{.Name}}{{end}}))
} }
{{- end -}} {{ end }}{{ end }}`
{{- end -}} methodDefinition = `{{ define "METHOD" }}{{ if eq .ReturnType "bool"}}
{{- define "METHOD" -}} func scriptFor{{.Name}}({{range $index, $arg := .Arguments -}}
{{- if eq .ReturnType "bool"}}func scriptFor{{.Name}}({{range $index, $arg := .Arguments -}}
{{- if ne $index 0}}, {{end}} {{- if ne $index 0}}, {{end}}
{{- .Name}} {{.Type}} {{- .Name}} {{.Type}}
{{- end}}) ([]byte, error) { {{- end}}) ([]byte, error) {
return smartcontract.CreateCallWithAssertScript(Hash, "{{ .NameABI }}"{{- range $index, $arg := .Arguments -}}, {{.Name}}{{end}}) return smartcontract.CreateCallWithAssertScript(Hash, "{{ .NameABI }}"{{- range $index, $arg := .Arguments -}}, {{.Name}}{{end}})
} }
{{ end }}
{{end}}// {{.Name}} {{.Comment}} // {{.Name}} {{.Comment}}
// This transaction is signed and immediately sent to the network. // This transaction is signed and immediately sent to the network.
// The values returned are its hash, ValidUntilBlock value and error if any. // The values returned are its hash, ValidUntilBlock value and error if any.
func (c *Contract) {{.Name}}({{range $index, $arg := .Arguments -}} func (c *Contract) {{.Name}}({{range $index, $arg := .Arguments -}}
@ -97,8 +99,9 @@ func (c *Contract) {{.Name}}Unsigned({{range $index, $arg := .Arguments -}}
} }
return c.actor.MakeUnsignedRun(script, nil){{end}} return c.actor.MakeUnsignedRun(script, nil){{end}}
} }
{{- end -}} {{end}}`
// Package {{.PackageName}} contains RPC wrappers for {{.ContractName}} contract.
bindingDefinition = `// Package {{.PackageName}} contains RPC wrappers for {{.ContractName}} contract.
package {{.PackageName}} package {{.PackageName}}
import ( import (
@ -107,7 +110,6 @@ import (
// Hash contains contract hash. // Hash contains contract hash.
var Hash = {{ .Hash }} var Hash = {{ .Hash }}
{{ range $name, $typ := .NamedTypes }} {{ range $name, $typ := .NamedTypes }}
// {{toTypeName $name}} is a contract-specific {{$name}} type used by its methods. // {{toTypeName $name}} is a contract-specific {{$name}} type used by its methods.
type {{toTypeName $name}} struct { type {{toTypeName $name}} struct {
@ -115,8 +117,9 @@ type {{toTypeName $name}} struct {
{{.Field}} {{etTypeToStr .ExtendedType}} {{.Field}} {{etTypeToStr .ExtendedType}}
{{- end}} {{- end}}
} }
{{end -}} {{end}}
{{if .HasReader}}// Invoker is used by ContractReader to call various safe methods. {{- if .HasReader}}
// Invoker is used by ContractReader to call various safe methods.
type Invoker interface { type Invoker interface {
{{if or .IsNep11D .IsNep11ND}} nep11.Invoker {{if or .IsNep11D .IsNep11ND}} nep11.Invoker
{{else -}} {{else -}}
@ -129,9 +132,9 @@ type Invoker interface {
{{end -}} {{end -}}
{{end -}} {{end -}}
} }
{{end -}} {{end -}}
{{if .HasWriter}}// Actor is used by Contract to call state-changing methods. {{- if .HasWriter}}
// Actor is used by Contract to call state-changing methods.
type Actor interface { type Actor interface {
{{- if .HasReader}} {{- if .HasReader}}
Invoker Invoker
@ -150,9 +153,9 @@ type Actor interface {
SendRun(script []byte) (util.Uint256, uint32, error) SendRun(script []byte) (util.Uint256, uint32, error)
{{end -}} {{end -}}
} }
{{end -}} {{end -}}
{{if .HasReader}}// ContractReader implements safe contract methods. {{- if .HasReader}}
// ContractReader implements safe contract methods.
type ContractReader struct { type ContractReader struct {
{{if .IsNep11D}}nep11.DivisibleReader {{if .IsNep11D}}nep11.DivisibleReader
{{end -}} {{end -}}
@ -162,9 +165,9 @@ type ContractReader struct {
{{end -}} {{end -}}
invoker Invoker invoker Invoker
} }
{{end -}} {{end -}}
{{if .HasWriter}}// Contract implements all contract methods. {{- if .HasWriter}}
// Contract implements all contract methods.
type Contract struct { type Contract struct {
{{if .HasReader}}ContractReader {{if .HasReader}}ContractReader
{{end -}} {{end -}}
@ -176,9 +179,9 @@ type Contract struct {
{{end -}} {{end -}}
actor Actor actor Actor
} }
{{end -}} {{end -}}
{{if .HasReader}}// NewReader creates an instance of ContractReader using Hash and the given Invoker. {{- if .HasReader}}
// NewReader creates an instance of ContractReader using Hash and the given Invoker.
func NewReader(invoker Invoker) *ContractReader { func NewReader(invoker Invoker) *ContractReader {
return &ContractReader{ return &ContractReader{
{{- if .IsNep11D}}*nep11.NewDivisibleReader(invoker, Hash), {{end}} {{- if .IsNep11D}}*nep11.NewDivisibleReader(invoker, Hash), {{end}}
@ -186,9 +189,9 @@ func NewReader(invoker Invoker) *ContractReader {
{{- if .IsNep17}}*nep17.NewReader(invoker, Hash), {{end -}} {{- if .IsNep17}}*nep17.NewReader(invoker, Hash), {{end -}}
invoker} invoker}
} }
{{end -}} {{end -}}
{{if .HasWriter}}// New creates an instance of Contract using Hash and the given Actor. {{- if .HasWriter}}
// New creates an instance of Contract using Hash and the given Actor.
func New(actor Actor) *Contract { func New(actor Actor) *Contract {
{{if .IsNep11D}}var nep11dt = nep11.NewDivisible(actor, Hash) {{if .IsNep11D}}var nep11dt = nep11.NewDivisible(actor, Hash)
{{end -}} {{end -}}
@ -207,14 +210,9 @@ func New(actor Actor) *Contract {
{{- if .IsNep17}}nep17t.TokenWriter, {{end -}} {{- if .IsNep17}}nep17t.TokenWriter, {{end -}}
actor} actor}
} }
{{end -}} {{end -}}
{{range $m := .SafeMethods}} {{- range $m := .SafeMethods }}{{template "SAFEMETHOD" $m }}{{ end -}}
{{template "SAFEMETHOD" $m }} {{- range $m := .Methods -}}{{template "METHOD" $m }}{{ end -}}
{{end}}
{{- range $m := .Methods}}
{{template "METHOD" $m }}
{{end}}
{{- range $name, $typ := .NamedTypes }} {{- range $name, $typ := .NamedTypes }}
// itemTo{{toTypeName $name}} converts stack item into *{{toTypeName $name}}. // itemTo{{toTypeName $name}} converts stack item into *{{toTypeName $name}}.
func itemTo{{toTypeName $name}}(item stackitem.Item, err error) (*{{toTypeName $name}}, error) { func itemTo{{toTypeName $name}}(item stackitem.Item, err error) (*{{toTypeName $name}}, error) {
@ -236,8 +234,8 @@ func (res *{{toTypeName $name}}) FromStackItem(item stackitem.Item) error {
if len(arr) != {{len $typ.Fields}} { if len(arr) != {{len $typ.Fields}} {
return errors.New("wrong number of structure elements") return errors.New("wrong number of structure elements")
} }
{{if len .Fields}}
{{if len .Fields}} var ( var (
index = -1 index = -1
err error err error
) )
@ -248,10 +246,15 @@ func (res *{{toTypeName $name}}) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field {{.Field}}: %w", err) return fmt.Errorf("field {{.Field}}: %w", err)
} }
{{end}} {{end}}
{{end}} {{- end}}
return nil return nil
} }
{{end}}` {{end -}}`
srcTmpl = bindingDefinition +
safemethodDefinition +
methodDefinition
)
type ( type (
ContractTmpl struct { ContractTmpl struct {