本文整理汇总了Golang中github.com/raphael/goa/goagen/codegen.NewGoGenerator函数的典型用法代码示例。如果您正苦于以下问题:Golang NewGoGenerator函数的具体用法?Golang NewGoGenerator怎么用?Golang NewGoGenerator使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewGoGenerator函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewControllersWriter
// NewControllersWriter returns a handlers code writer.
// Handlers provide the glue between the underlying request data and the user controller.
func NewControllersWriter(filename string) (*ControllersWriter, error) {
cw := codegen.NewGoGenerator(filename)
funcMap := cw.FuncMap
funcMap["add"] = func(a, b int) int { return a + b }
funcMap["gotypename"] = codegen.GoTypeName
ctrlTmpl, err := template.New("controller").Funcs(funcMap).Parse(ctrlT)
if err != nil {
return nil, err
}
mountTmpl, err := template.New("mount").Funcs(funcMap).Parse(mountT)
if err != nil {
return nil, err
}
unmarshalTmpl, err := template.New("unmarshal").Funcs(funcMap).Parse(unmarshalT)
if err != nil {
return nil, err
}
w := ControllersWriter{
GoGenerator: cw,
CtrlTmpl: ctrlTmpl,
MountTmpl: mountTmpl,
UnmarshalTmpl: unmarshalTmpl,
}
return &w, nil
}
示例2: NewMediaTypesWriter
// NewMediaTypesWriter returns a contexts code writer.
// Media types contain the data used to render response bodies.
func NewMediaTypesWriter(filename string) (*MediaTypesWriter, error) {
cw := codegen.NewGoGenerator(filename)
funcMap := cw.FuncMap
funcMap["gotypedef"] = codegen.GoTypeDef
funcMap["gotyperef"] = codegen.GoTypeRef
funcMap["goify"] = codegen.Goify
funcMap["gotypename"] = codegen.GoTypeName
funcMap["gonative"] = codegen.GoNativeType
funcMap["typeUnmarshaler"] = codegen.TypeUnmarshaler
funcMap["typeMarshaler"] = codegen.MediaTypeMarshaler
funcMap["recursiveValidate"] = codegen.RecursiveChecker
funcMap["tempvar"] = codegen.Tempvar
funcMap["newDumpData"] = newDumpData
funcMap["userTypeUnmarshalerImpl"] = codegen.UserTypeUnmarshalerImpl
funcMap["mediaTypeMarshalerImpl"] = codegen.MediaTypeMarshalerImpl
mediaTypeTmpl, err := template.New("media type").Funcs(funcMap).Parse(mediaTypeT)
if err != nil {
return nil, err
}
w := MediaTypesWriter{
GoGenerator: cw,
MediaTypeTmpl: mediaTypeTmpl,
}
return &w, nil
}
示例3: generateClientResources
func (g *Generator) generateClientResources(clientPkg string, funcs template.FuncMap, api *design.APIDefinition) error {
clientsTmpl := template.Must(template.New("clients").Funcs(funcs).Parse(clientsTmpl))
imports := []*codegen.ImportSpec{
codegen.SimpleImport("bytes"),
codegen.SimpleImport("encoding/json"),
codegen.SimpleImport("fmt"),
codegen.SimpleImport("net/http"),
}
return api.IterateResources(func(res *design.ResourceDefinition) error {
filename := filepath.Join(codegen.OutputDir, snakeCase(res.Name)+".go")
resGen := codegen.NewGoGenerator(filename)
if err := resGen.WriteHeader("", "client", imports); err != nil {
return err
}
g.genfiles = append(g.genfiles, filename)
if err := res.IterateActions(func(action *design.ActionDefinition) error {
return clientsTmpl.Execute(resGen, action)
}); err != nil {
return err
}
return resGen.FormatCode()
})
}
示例4: generateToolSourceCode
func (m *Generator) generateToolSourceCode(gendir, pkgName string) {
filename := filepath.Join(gendir, "main.go")
m.GoGenerator = codegen.NewGoGenerator(filename)
imports := append(m.Imports,
codegen.SimpleImport("fmt"),
codegen.SimpleImport("os"),
codegen.SimpleImport("strings"),
codegen.NewImport(".", "github.com/raphael/goa/design"),
codegen.NewImport(".", "github.com/raphael/goa/design/dsl"),
codegen.NewImport("_", codegen.DesignPackagePath),
)
m.WriteHeader("Code Generator", "main", imports)
tmpl, err := template.New("generator").Parse(mainTmpl)
if err != nil {
panic(err) // bug
}
context := map[string]string{
"Genfunc": m.Genfunc,
"DesignPackage": codegen.DesignPackagePath,
"PkgName": pkgName,
}
err = tmpl.Execute(m, context)
if err != nil {
panic(err) // bug
}
if codegen.Debug {
src, _ := ioutil.ReadFile(filename)
fmt.Printf("goagen source:\n%s\n", src)
}
}
示例5: Generate
// Generate is the generator entry point called by the meta generator.
func Generate(api *design.APIDefinition) (_ []string, err error) {
var genfiles []string
cleanup := func() {
for _, f := range genfiles {
os.Remove(f)
}
}
go utils.Catch(nil, cleanup)
defer func() {
if err != nil {
cleanup()
}
}()
app := kingpin.New("Swagger generator", "Swagger spec generator")
codegen.RegisterFlags(app)
_, err = app.Parse(os.Args[1:])
if err != nil {
return nil, fmt.Errorf(`invalid command line: %s. Command line was "%s"`,
err, strings.Join(os.Args, " "))
}
s, err := New(api)
if err != nil {
return
}
b, err := json.Marshal(s)
if err != nil {
return
}
swaggerDir := filepath.Join(codegen.OutputDir, "swagger")
os.RemoveAll(swaggerDir)
if err = os.MkdirAll(swaggerDir, 0755); err != nil {
return
}
genfiles = append(genfiles, swaggerDir)
swaggerFile := filepath.Join(swaggerDir, "swagger.json")
err = ioutil.WriteFile(swaggerFile, b, 0644)
if err != nil {
return
}
genfiles = append(genfiles, swaggerFile)
controllerFile := filepath.Join(swaggerDir, "swagger.go")
genfiles = append(genfiles, controllerFile)
gg := codegen.NewGoGenerator(controllerFile)
imports := []*codegen.ImportSpec{
codegen.SimpleImport("github.com/julienschmidt/httprouter"),
codegen.SimpleImport("github.com/raphael/goa"),
}
gg.WriteHeader(fmt.Sprintf("%s Swagger Spec", api.Name), "swagger", imports)
gg.Write([]byte(swagger))
if err = gg.FormatCode(); err != nil {
return
}
return genfiles, nil
}
示例6: NewGenerator
// NewGenerator returns the application code generator.
func NewGenerator() (*Generator, error) {
app := kingpin.New("Code generator", "application code generator")
codegen.RegisterFlags(app)
NewCommand().RegisterFlags(app)
_, err := app.Parse(os.Args[1:])
if err != nil {
return nil, fmt.Errorf(`invalid command line: %s. Command line was "%s"`,
err, strings.Join(os.Args, " "))
}
outdir := AppOutputDir()
os.RemoveAll(outdir)
if err = os.MkdirAll(outdir, 0777); err != nil {
return nil, err
}
ctxFile := filepath.Join(outdir, "contexts.go")
ctlFile := filepath.Join(outdir, "controllers.go")
resFile := filepath.Join(outdir, "hrefs.go")
mtFile := filepath.Join(outdir, "media_types.go")
utFile := filepath.Join(outdir, "user_types.go")
ctxWr, err := NewContextsWriter(ctxFile)
if err != nil {
panic(err) // bug
}
ctlWr, err := NewControllersWriter(ctlFile)
if err != nil {
panic(err) // bug
}
resWr, err := NewResourcesWriter(resFile)
if err != nil {
panic(err) // bug
}
mtWr, err := NewMediaTypesWriter(mtFile)
if err != nil {
panic(err) // bug
}
utWr, err := NewUserTypesWriter(utFile)
if err != nil {
panic(err) // bug
}
return &Generator{
GoGenerator: codegen.NewGoGenerator(outdir),
ContextsWriter: ctxWr,
ControllersWriter: ctlWr,
ResourcesWriter: resWr,
MediaTypesWriter: mtWr,
UserTypesWriter: utWr,
contextsFilename: ctxFile,
controllersFilename: ctlFile,
resourcesFilename: resFile,
mediaTypesFilename: mtFile,
userTypesFilename: utFile,
genfiles: []string{outdir},
}, nil
}
示例7: NewResourcesWriter
// NewResourcesWriter returns a contexts code writer.
// Resources provide the glue between the underlying request data and the user controller.
func NewResourcesWriter(filename string) (*ResourcesWriter, error) {
cw := codegen.NewGoGenerator(filename)
funcMap := cw.FuncMap
funcMap["join"] = strings.Join
resourceTmpl, err := template.New("resource").Funcs(cw.FuncMap).Parse(resourceT)
if err != nil {
return nil, err
}
w := ResourcesWriter{
GoGenerator: cw,
ResourceTmpl: resourceTmpl,
}
return &w, nil
}
示例8: NewContextsWriter
// NewContextsWriter returns a contexts code writer.
// Contexts provide the glue between the underlying request data and the user controller.
func NewContextsWriter(filename string) (*ContextsWriter, error) {
cw := codegen.NewGoGenerator(filename)
funcMap := cw.FuncMap
funcMap["gotyperef"] = codegen.GoTypeRef
funcMap["gotypedef"] = codegen.GoTypeDef
funcMap["goify"] = codegen.Goify
funcMap["gotypename"] = codegen.GoTypeName
funcMap["gopkgtypename"] = codegen.GoPackageTypeName
funcMap["typeUnmarshaler"] = codegen.TypeUnmarshaler
funcMap["userTypeUnmarshalerImpl"] = codegen.UserTypeUnmarshalerImpl
funcMap["validationChecker"] = codegen.ValidationChecker
funcMap["tabs"] = codegen.Tabs
funcMap["add"] = func(a, b int) int { return a + b }
funcMap["gopkgtyperef"] = codegen.GoPackageTypeRef
ctxTmpl, err := template.New("context").Funcs(funcMap).Parse(ctxT)
if err != nil {
return nil, err
}
ctxNewTmpl, err := template.New("new").
Funcs(cw.FuncMap).
Funcs(template.FuncMap{
"newCoerceData": newCoerceData,
"arrayAttribute": arrayAttribute,
}).Parse(ctxNewT)
if err != nil {
return nil, err
}
ctxRespTmpl, err := template.New("response").Funcs(cw.FuncMap).Parse(ctxRespT)
if err != nil {
return nil, err
}
payloadTmpl, err := template.New("payload").Funcs(funcMap).Parse(payloadT)
if err != nil {
return nil, err
}
newPayloadTmpl, err := template.New("newpayload").Funcs(cw.FuncMap).Parse(newPayloadT)
if err != nil {
return nil, err
}
w := ContextsWriter{
GoGenerator: cw,
CtxTmpl: ctxTmpl,
CtxNewTmpl: ctxNewTmpl,
CtxRespTmpl: ctxRespTmpl,
PayloadTmpl: payloadTmpl,
NewPayloadTmpl: newPayloadTmpl,
}
return &w, nil
}
示例9: Generate
// Generate is the generator entry point called by the meta generator.
func Generate(api *design.APIDefinition) ([]string, error) {
app := kingpin.New("Swagger generator", "Swagger spec generator")
codegen.RegisterFlags(app)
_, err := app.Parse(os.Args[1:])
if err != nil {
return nil, fmt.Errorf(`invalid command line: %s. Command line was "%s"`,
err, strings.Join(os.Args, " "))
}
s, err := New(api)
if err != nil {
return nil, err
}
b, err := json.Marshal(s)
if err != nil {
return nil, err
}
swaggerDir := filepath.Join(codegen.OutputDir, "swagger")
os.RemoveAll(swaggerDir)
if err = os.MkdirAll(swaggerDir, 0755); err != nil {
return nil, err
}
swaggerFile := filepath.Join(swaggerDir, "swagger.json")
err = ioutil.WriteFile(swaggerFile, b, 0644)
if err != nil {
return nil, err
}
controllerFile := filepath.Join(swaggerDir, "swagger.go")
tmpl, err := template.New("swagger").Parse(swaggerTmpl)
if err != nil {
panic(err.Error()) // bug
}
gg := codegen.NewGoGenerator(controllerFile)
imports := []*codegen.ImportSpec{
codegen.SimpleImport("github.com/raphael/goa"),
}
gg.WriteHeader(fmt.Sprintf("%s Swagger Spec", api.Name), "swagger", imports)
data := map[string]interface{}{
"spec": string(b),
}
err = tmpl.Execute(gg, data)
if err != nil {
return nil, err
}
if err := gg.FormatCode(); err != nil {
return nil, err
}
return []string{controllerFile, swaggerFile}, nil
}
示例10: NewUserTypesWriter
// NewUserTypesWriter returns a contexts code writer.
// User types contain custom data structured defined in the DSL with "Type".
func NewUserTypesWriter(filename string) (*UserTypesWriter, error) {
cw := codegen.NewGoGenerator(filename)
funcMap := cw.FuncMap
funcMap["gotypedef"] = codegen.GoTypeDef
funcMap["goify"] = codegen.Goify
funcMap["gotypename"] = codegen.GoTypeName
userTypeTmpl, err := template.New("user type").Funcs(funcMap).Parse(userTypeT)
if err != nil {
return nil, err
}
w := UserTypesWriter{
GoGenerator: cw,
UserTypeTmpl: userTypeTmpl,
}
return &w, nil
}
示例11: generateMain
func (g *Generator) generateMain(mainFile string, clientPkg string, funcs template.FuncMap, api *design.APIDefinition) error {
gg := codegen.NewGoGenerator(mainFile)
mainTmpl := template.Must(template.New("main").Funcs(funcs).Parse(mainTmpl))
registerCmdsTmpl := template.Must(template.New("registerCmds").Funcs(funcs).Parse(registerCmdsT))
imports := []*codegen.ImportSpec{
codegen.SimpleImport("os"),
codegen.SimpleImport(clientPkg),
codegen.SimpleImport("gopkg.in/alecthomas/kingpin.v2"),
}
for _, pkg := range SignerPackages {
imports = append(imports, codegen.SimpleImport(pkg))
}
if err := gg.WriteHeader("", "main", imports); err != nil {
return err
}
g.genfiles = append(g.genfiles, mainFile)
data := map[string]interface{}{
"API": api,
"Signers": Signers,
"Version": Version,
}
if err := mainTmpl.Execute(gg, data); err != nil {
return err
}
actions := make(map[string][]*design.ActionDefinition)
api.IterateResources(func(res *design.ResourceDefinition) error {
return res.IterateActions(func(action *design.ActionDefinition) error {
if as, ok := actions[action.Name]; ok {
actions[action.Name] = append(as, action)
} else {
actions[action.Name] = []*design.ActionDefinition{action}
}
return nil
})
})
if err := registerCmdsTmpl.Execute(gg, actions); err != nil {
return err
}
return gg.FormatCode()
}
示例12: NewGenerator
// NewGenerator returns the application code generator.
func NewGenerator() (*Generator, error) {
app := kingpin.New("Code generator", "application code generator")
codegen.RegisterFlags(app)
NewCommand().RegisterFlags(app)
_, err := app.Parse(os.Args[1:])
if err != nil {
return nil, fmt.Errorf(`invalid command line: %s. Command line was "%s"`,
err, strings.Join(os.Args, " "))
}
outdir := AppOutputDir()
os.RemoveAll(outdir)
if err = os.MkdirAll(outdir, 0777); err != nil {
return nil, err
}
return &Generator{
GoGenerator: codegen.NewGoGenerator(outdir),
genfiles: []string{outdir},
}, nil
}
示例13: NewUserTypesWriter
// NewUserTypesWriter returns a contexts code writer.
// User types contain custom data structured defined in the DSL with "Type".
func NewUserTypesWriter(filename string) (*UserTypesWriter, error) {
cw := codegen.NewGoGenerator(filename)
funcMap := cw.FuncMap
funcMap["gotypedef"] = codegen.GoTypeDef
funcMap["gotyperef"] = codegen.GoTypeRef
funcMap["goify"] = codegen.Goify
funcMap["gotypename"] = codegen.GoTypeName
funcMap["recursiveValidate"] = codegen.RecursiveChecker
funcMap["userTypeUnmarshalerImpl"] = codegen.UserTypeUnmarshalerImpl
funcMap["userTypeMarshalerImpl"] = codegen.UserTypeMarshalerImpl
userTypeTmpl, err := template.New("user type").Funcs(funcMap).Parse(userTypeT)
if err != nil {
return nil, err
}
w := UserTypesWriter{
GoGenerator: cw,
UserTypeTmpl: userTypeTmpl,
}
return &w, nil
}
示例14: generateClient
func (g *Generator) generateClient(clientFile string, clientPkg string, funcs template.FuncMap, api *design.APIDefinition) error {
gg := codegen.NewGoGenerator(clientFile)
clientTmpl := template.Must(template.New("client").Funcs(funcs).Parse(clientTmpl))
imports := []*codegen.ImportSpec{
codegen.SimpleImport("net/http"),
codegen.SimpleImport("github.com/raphael/goa"),
codegen.SimpleImport("gopkg.in/alecthomas/kingpin.v2"),
}
if err := gg.WriteHeader("", "client", imports); err != nil {
return err
}
g.genfiles = append(g.genfiles, clientFile)
if err := clientTmpl.Execute(gg, api); err != nil {
return err
}
return gg.FormatCode()
}
示例15: generateCommands
func (g *Generator) generateCommands(commandsFile string, clientPkg string, funcs template.FuncMap, api *design.APIDefinition) error {
gg := codegen.NewGoGenerator(commandsFile)
commandTypesTmpl := template.Must(template.New("commandTypes").Funcs(funcs).Parse(commandTypesTmpl))
commandsTmpl := template.Must(template.New("commands").Funcs(funcs).Parse(commandsTmpl))
imports := []*codegen.ImportSpec{
codegen.SimpleImport("github.com/raphael/goa"),
codegen.SimpleImport(clientPkg),
codegen.NewImport("log", "gopkg.in/inconshreveable/log15.v2"),
codegen.SimpleImport("gopkg.in/alecthomas/kingpin.v2"),
}
if err := gg.WriteHeader("", "main", imports); err != nil {
return err
}
g.genfiles = append(g.genfiles, commandsFile)
gg.Write([]byte("type (\n"))
if err := api.IterateResources(func(res *design.ResourceDefinition) error {
return res.IterateActions(func(action *design.ActionDefinition) error {
return commandTypesTmpl.Execute(gg, action)
})
}); err != nil {
return err
}
gg.Write([]byte(")\n\n"))
if err := api.IterateResources(func(res *design.ResourceDefinition) error {
return res.IterateActions(func(action *design.ActionDefinition) error {
data := map[string]interface{}{
"Action": action,
"Resource": action.Parent,
"Version": design.Design.APIVersionDefinition,
}
return commandsTmpl.Execute(gg, data)
})
}); err != nil {
return err
}
return gg.FormatCode()
}