smartcontract: apply gofmt to the resulting bindings

Close #3133.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2023-10-18 17:58:39 +03:00
parent d24579748e
commit c42486587d
16 changed files with 769 additions and 742 deletions

View file

@ -3,6 +3,7 @@ package binding
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"go/format"
"go/token" "go/token"
"io" "io"
"sort" "sort"
@ -127,7 +128,32 @@ func Generate(cfg Config) error {
ctr.Imports = append(ctr.Imports, "github.com/nspcc-dev/neo-go/pkg/interop/neogointernal") ctr.Imports = append(ctr.Imports, "github.com/nspcc-dev/neo-go/pkg/interop/neogointernal")
sort.Strings(ctr.Imports) sort.Strings(ctr.Imports)
return srcTemplate.Execute(cfg.Output, ctr) return FExecute(srcTemplate, cfg.Output, ctr)
}
// FExecute tries to execute given template over the data provided, apply gofmt
// rules to the result and write the result to the provided io.Writer. If a
// format error occurs while formatting the resulting binding, then the generated
// binding is written "as is" and no error is returned.
func FExecute(tmplt *template.Template, out io.Writer, data any) error {
in := bytes.NewBuffer(nil)
err := tmplt.Execute(in, data)
if err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
res := in.Bytes()
fmtRes, err := format.Source(res)
if err != nil {
// OK, still write something to the resulting file, our generator has known
// bugs that make the resulting code uncompilable.
fmtRes = res
}
_, err = out.Write(fmtRes)
if err != nil {
return fmt.Errorf("failed to write the resulting binding: %w", err)
}
return nil
} }
func scTypeToGo(name string, typ smartcontract.ParamType, cfg *Config) (string, string) { func scTypeToGo(name string, typ smartcontract.ParamType, cfg *Config) (string, string) {

View file

@ -464,7 +464,7 @@ func Generate(cfg binding.Config) error {
"upperFirst": upperFirst, "upperFirst": upperFirst,
}).Parse(srcTmpl)) }).Parse(srcTmpl))
return srcTemplate.Execute(cfg.Output, ctr) return binding.FExecute(srcTemplate, cfg.Output, ctr)
} }
func dropManifestMethods(meths []manifest.Method, manifested []manifest.Method) []manifest.Method { func dropManifestMethods(meths []manifest.Method, manifested []manifest.Method) []manifest.Method {

View file

@ -21,6 +21,7 @@ import (
"github.com/consensys/gnark/backend/groth16" "github.com/consensys/gnark/backend/groth16"
curve "github.com/consensys/gnark/backend/groth16/bls12-381" curve "github.com/consensys/gnark/backend/groth16/bls12-381"
"github.com/consensys/gnark/backend/witness" "github.com/consensys/gnark/backend/witness"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/binding"
"github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/util/slice"
) )
@ -234,7 +235,7 @@ func GenerateVerifier(cfg Config) error {
"byteSliceToStr": byteSliceToStr, "byteSliceToStr": byteSliceToStr,
}).Parse(goVerificationTmpl)) }).Parse(goVerificationTmpl))
err := tmpl.Execute(cfg.Output, tmplParams{ err := binding.FExecute(tmpl, cfg.Output, tmplParams{
Alpha: alphaG1[:], Alpha: alphaG1[:],
Beta: betaG2[:], Beta: betaG2[:],
Gamma: gammaG2[:], Gamma: gammaG2[:],
@ -242,7 +243,7 @@ func GenerateVerifier(cfg Config) error {
ICs: kvks, ICs: kvks,
}) })
if err != nil { if err != nil {
return fmt.Errorf("failed to generate template: %w", err) return err
} }
if cfg.CfgOutput != nil { if cfg.CfgOutput != nil {