Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Sep 6, 2023
1 parent ab12302 commit 05fb779
Showing 1 changed file with 25 additions and 42 deletions.
67 changes: 25 additions & 42 deletions consensus/polybft/contractsapi/bindings-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,20 +785,43 @@ func generateFunction(generatedData *generatedData, contractName string,
}

// write encode/decode functions
tmplStr := generateFunctionTemplate(fnSigResolution)

tmplString := `
{{range .Structs}}
{{.}}
{{ end }}
func ({{.Sig}} *{{.TName}}) Sig() []byte {
return {{.ContractName}}.Abi.{{.MethodGetter}}["{{.Name}}"].ID()
}
func ({{.Sig}} *{{.TName}}) EncodeAbi() ([]byte, error) {
return {{.ContractName}}.Abi.{{.MethodGetter}}["{{.Name}}"].Encode({{.Sig}})
}
func ({{.Sig}} *{{.TName}}) DecodeAbi(buf []byte) error {
return decodeMethod({{.ContractName}}.Abi.{{.MethodGetter}}["{{.Name}}"], buf, {{.Sig}})
}`

methodGetter := "Methods"
if fnSigResolution {
methodGetter = "MethodsBySignature"
}

inputs := map[string]interface{}{
"Structs": res,
"Sig": strings.ToLower(string(methodName[0])),
"Name": method.Name,
"ContractName": contractName,
"TName": strings.Title(methodName),
"MethodGetter": methodGetter,
}

if fnSigResolution {
inputs["Name"] = method.Sig()
}

renderedString, err := renderTmpl(tmplStr, inputs)
renderedString, err := renderTmpl(tmplString, inputs)
if err != nil {
return err
}
Expand All @@ -808,46 +831,6 @@ func generateFunction(generatedData *generatedData, contractName string,
return nil
}

// generateFunctionTemplate generates function template string, based on provided flag
// depending whether function is resolved by signature or by name
func generateFunctionTemplate(fnSigResolution bool) string {
if fnSigResolution {
return `
{{range .Structs}}
{{.}}
{{ end }}
func ({{.Sig}} *{{.TName}}) Sig() []byte {
return {{.ContractName}}.Abi.MethodsBySignature["{{.Name}}"].ID()
}
func ({{.Sig}} *{{.TName}}) EncodeAbi() ([]byte, error) {
return {{.ContractName}}.Abi.MethodsBySignature["{{.Name}}"].Encode({{.Sig}})
}
func ({{.Sig}} *{{.TName}}) DecodeAbi(buf []byte) error {
return decodeMethod({{.ContractName}}.Abi.MethodsBySignature["{{.Name}}"], buf, {{.Sig}})
}`
}

return `
{{range .Structs}}
{{.}}
{{ end }}
func ({{.Sig}} *{{.TName}}) Sig() []byte {
return {{.ContractName}}.Abi.Methods["{{.Name}}"].ID()
}
func ({{.Sig}} *{{.TName}}) EncodeAbi() ([]byte, error) {
return {{.ContractName}}.Abi.Methods["{{.Name}}"].Encode({{.Sig}})
}
func ({{.Sig}} *{{.TName}}) DecodeAbi(buf []byte) error {
return decodeMethod({{.ContractName}}.Abi.Methods["{{.Name}}"], buf, {{.Sig}})
}`
}

func renderTmpl(tmplStr string, inputs map[string]interface{}) (string, error) {
tmpl, err := template.New("name").Parse(tmplStr)
if err != nil {
Expand Down

0 comments on commit 05fb779

Please sign in to comment.