Skip to content

Instantly share code, notes, and snippets.

@archishou
Created October 31, 2019 20:00
Show Gist options
  • Save archishou/53f89a1c033746462c6c9ea13079df5c to your computer and use it in GitHub Desktop.
Save archishou/53f89a1c033746462c6c9ea13079df5c to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
. "github.com/dave/jennifer/jen"
"unicode"
)
func main() {
var name string
flag.StringVar(&name, "name", "", "Usage")
flag.Parse()
if name == "" {
fmt.Println("title is unspecified")
return
}
a := []rune(name)
a[0] = unicode.ToLower(a[0])
camelCase := string(a)
f := NewFile("pubsubtypes")
f.ImportName("github.com/lf-edge/eve/pkg/pillar/pubsub", "pubsub")
f.ImportName("github.com/lf-edge/eve/pkg/pillar/types", "types")
f.ImportName("log", "log")
f.Func().Id("CallbackFn").Params(
Id("types." + name),
).Bool()
f.Type().Id(name+"PubSubBase").Struct(
Id("agentName").String(),
Id("db").Map(String()).Id("types."+name),
)
f.Func().Params(
Id("config").Id("*" + name + "PubSubBase"),
).Id("Get").Params(
Id("key").String(),
).Id("types." + name).Block(
If(
List(Id("res"), Id("ok")).Op(":=").Id("config.db[key];").Id("ok"),
).Block(
Return(Id("res")),
).Else().Block(
Return(Id("types." + name + "{}")),
),
)
f.Func().Params(
Id("config").Id("*"+name+"PubSubBase"),
).Id("Set").Params(
Id("key").String(),
Id(camelCase).Id("types."+name),
).Block(
Id("config.db[key]").Op("=").Id(camelCase),
)
fmt.Printf("%#v", f)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment