summaryrefslogtreecommitdiff
path: root/pkg/bindings/generator/generator.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2021-09-14 13:04:11 -0400
committerGitHub <noreply@github.com>2021-09-14 13:04:11 -0400
commitbb8b2ed7deb71431521d24486b0856845e7a7867 (patch)
tree3afa51a80166d3033bd1c793f1812bb7c9953155 /pkg/bindings/generator/generator.go
parent20daae1de93c1a73f454f3f3ba04bba03a348f81 (diff)
parentd7256be807890892c988cc7d270e3d1bf951397c (diff)
downloadpodman-bb8b2ed7deb71431521d24486b0856845e7a7867.tar.gz
podman-bb8b2ed7deb71431521d24486b0856845e7a7867.tar.bz2
podman-bb8b2ed7deb71431521d24486b0856845e7a7867.zip
Merge pull request #11559 from jwhonce/wip/generator
Enhance bindings for IDE hints
Diffstat (limited to 'pkg/bindings/generator/generator.go')
-rw-r--r--pkg/bindings/generator/generator.go206
1 files changed, 103 insertions, 103 deletions
diff --git a/pkg/bindings/generator/generator.go b/pkg/bindings/generator/generator.go
index 90f0bfaf5..f7388ec6a 100644
--- a/pkg/bindings/generator/generator.go
+++ b/pkg/bindings/generator/generator.go
@@ -1,5 +1,10 @@
+// +build ignore
+
package main
+// This program generates *_options_.go files to be used by the bindings calls to API service.
+// It can be invoked by running go generate
+
import (
"errors"
"fmt"
@@ -11,56 +16,52 @@ import (
"os/exec"
"strings"
"text/template"
- "time"
+ "unicode"
+ "unicode/utf8"
)
-var bodyTmpl = `package {{.PackageName}}
+var bodyTmpl = `// Code generated by go generate; DO NOT EDIT.
+package {{.PackageName}}
import (
{{range $import := .Imports}} {{$import}}
{{end}}
)
-/*
-This file is generated automatically by go generate. Do not edit.
-*/
-
-// Changed
+// Changed returns true if named field has been set
func (o *{{.StructName}}) Changed(fieldName string) bool {
return util.Changed(o, fieldName)
}
-// ToParams
+// ToParams formats struct fields to be passed to API service
func (o *{{.StructName}}) ToParams() (url.Values, error) {
return util.ToParams(o)
}
{{range $field := .Fields}}
-// With{{.Name}}
-func(o *{{$field.StructName}}) With{{$field.Name}}(value {{$field.Type}}) *{{$field.StructName}} {
- v := {{$field.TypedValue}}
- o.{{$field.Name}} = v
+// With{{.Name}} set {{if .Comment}}{{.Comment}}{{else}}field {{.Name}} to given value{{end}}
+func(o *{{.StructName}}) With{{.Name}}(value {{.Type}}) *{{.StructName}} {
+ o.{{.Name}} = {{if not .Composite}}&{{end}}value
return o
}
-// Get{{.Name}}
-func(o *{{$field.StructName}}) Get{{$field.Name}}() {{$field.Type}} {
- var {{$field.ZeroName}} {{$field.Type}}
- if o.{{$field.Name}} == nil {
- return {{$field.ZeroName}}
+// Get{{.Name}} returns value of {{if .Comment}}{{.Comment}}{{else}}field {{.Name}}{{end}}
+func(o *{{.StructName}}) Get{{.Name}}() {{.Type}} {
+ if o.{{.Name}} == nil {
+ var z {{.Type}}
+ return z
}
- return {{$field.TypedName}}
+ return {{if not .Composite}}*{{end}}o.{{.Name}}
}
{{end}}
`
type fieldStruct struct {
+ Comment string
+ Composite bool
Name string
StructName string
Type string
- TypedName string
- TypedValue string
- ZeroName string
}
func main() {
@@ -69,7 +70,6 @@ func main() {
fieldStructs []fieldStruct
)
srcFile := os.Getenv("GOFILE")
- pkg := os.Getenv("GOPACKAGE")
inputStructName := os.Args[1]
b, err := ioutil.ReadFile(srcFile)
if err != nil {
@@ -80,6 +80,7 @@ func main() {
if err != nil {
panic(err)
}
+
// always add reflect
imports := []string{"\"reflect\"", "\"github.com/containers/podman/v3/pkg/bindings/internal/util\""}
for _, imp := range f.Imports {
@@ -96,95 +97,94 @@ func main() {
}
}()
+ body := template.Must(template.New("body").Parse(bodyTmpl))
+
ast.Inspect(f, func(n ast.Node) bool {
ref, refOK := n.(*ast.TypeSpec)
- if refOK {
- if ref.Name.Name == inputStructName {
- x := ref.Type.(*ast.StructType)
- for _, field := range x.Fields.List {
- var (
- name, zeroName, typedName, typedValue string
- )
- if len(field.Names) > 0 {
- name = field.Names[0].Name
- if len(name) < 1 {
- panic(errors.New("bad name"))
- }
- }
- for k, v := range name {
- zeroName = strings.ToLower(string(v)) + name[k+1:]
- break
- }
- //sub := "*"
- typeExpr := field.Type
- switch field.Type.(type) {
- case *ast.MapType, *ast.StructType, *ast.ArrayType:
- typedName = "o." + name
- typedValue = "value"
- default:
- typedName = "*o." + name
- typedValue = "&value"
- }
- start := typeExpr.Pos() - 1
- end := typeExpr.End() - 1
- fieldType := strings.Replace(string(b[start:end]), "*", "", 1)
- fStruct := fieldStruct{
- Name: name,
- StructName: inputStructName,
- Type: fieldType,
- TypedName: typedName,
- TypedValue: typedValue,
- ZeroName: zeroName,
- }
- fieldStructs = append(fieldStructs, fStruct)
- } // for
-
- bodyStruct := struct {
- PackageName string
- Imports []string
- Date string
- StructName string
- Fields []fieldStruct
- }{
- PackageName: pkg,
- Imports: imports,
- Date: time.Now().String(),
- StructName: inputStructName,
- Fields: fieldStructs,
+ if !(refOK && ref.Name.Name == inputStructName) {
+ return true
+ }
+
+ x := ref.Type.(*ast.StructType)
+ for _, field := range x.Fields.List {
+ var name string
+ if len(field.Names) > 0 {
+ name = field.Names[0].Name
+ if len(name) < 1 {
+ panic(errors.New("bad name"))
}
+ }
- body := template.Must(template.New("body").Parse(bodyTmpl))
+ var composite bool
+ switch field.Type.(type) {
+ case *ast.MapType, *ast.StructType, *ast.ArrayType:
+ composite = true
+ }
- // create the body
- if err := body.Execute(out, bodyStruct); err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
+ //sub := "*"
+ typeExpr := field.Type
+ start := typeExpr.Pos() - 1
+ end := typeExpr.End() - 1
+ fieldType := strings.Replace(string(b[start:end]), "*", "", 1)
+
+ fieldStructs = append(fieldStructs, fieldStruct{
+ Comment: fmtComment(field.Comment.Text()),
+ Composite: composite,
+ Name: name,
+ StructName: inputStructName,
+ Type: fieldType,
+ })
+ } // for
+
+ bodyStruct := struct {
+ PackageName string
+ Imports []string
+ StructName string
+ Fields []fieldStruct
+ }{
+ PackageName: os.Getenv("GOPACKAGE"),
+ Imports: imports,
+ StructName: inputStructName,
+ Fields: fieldStructs,
+ }
- // close out file
- if err := out.Close(); err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- closed = true
-
- // go fmt file
- gofmt := exec.Command("go", "fmt", out.Name())
- gofmt.Stderr = os.Stdout
- if err := gofmt.Run(); err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
+ // create the body
+ if err := body.Execute(out, bodyStruct); err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
- // go import file
- goimport := exec.Command("goimports", "-w", out.Name())
- goimport.Stderr = os.Stdout
- if err := goimport.Run(); err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- }
+ // close out file
+ if err := out.Close(); err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+ closed = true
+
+ // go fmt file
+ gofmt := exec.Command("go", "fmt", out.Name())
+ gofmt.Stderr = os.Stdout
+ if err := gofmt.Run(); err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+
+ // go import file
+ goimport := exec.Command("goimports", "-w", out.Name())
+ goimport.Stderr = os.Stdout
+ if err := goimport.Run(); err != nil {
+ fmt.Println(err)
+ os.Exit(1)
}
return true
})
}
+
+func fmtComment(comment string) string {
+ r, n := utf8.DecodeRuneInString(comment)
+ if r != utf8.RuneError {
+ comment = string(unicode.ToLower(r)) + comment[n:]
+ }
+ comment = strings.TrimSpace(comment)
+ return comment
+}