本文整理汇总了Golang中kego/io/process/generate/builder.Builder.Print方法的典型用法代码示例。如果您正苦于以下问题:Golang Builder.Print方法的具体用法?Golang Builder.Print怎么用?Golang Builder.Print使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kego/io/process/generate/builder.Builder
的用法示例。
在下文中一共展示了Builder.Print方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: printExportFunction
func printExportFunction(ctx context.Context, env *envctx.Env, g *builder.Builder, export *sysctx.SysExportInfo) error {
g.Println("func ", system.GoName(export.Name), "() *", g.SprintRef(export.TypePackage, system.GoName(export.TypeName)), " {")
{
g.Print("ctx := ")
g.PrintFunctionCall("kego.io/system", "NewContext",
g.SprintFunctionCall("context", "Background"),
strconv.Quote(env.Path),
fmt.Sprintf("%#v", env.Aliases),
)
g.Println()
g.Println("o := new(", g.SprintRef(export.TypePackage, system.GoName(export.TypeName)), ")")
g.Print("err := ")
g.PrintMethodCall("o", "Unpack",
"ctx",
g.SprintFunctionCall("kego.io/system", "MustPackString", strconv.Quote(string(export.JsonContents))),
"false",
)
g.Println()
g.Println("if err != nil {")
{
g.Println("panic(err.Error())")
}
g.Println("}")
g.Println("return o")
}
g.Println("}")
return nil
}
示例2: printInitFunction
func printInitFunction(ctx context.Context, env *envctx.Env, g *builder.Builder, types *sysctx.SysTypes) {
g.Println("func init() {")
{
g.Print("pkg := ")
g.PrintFunctionCall(
"kego.io/context/jsonctx",
"InitPackage",
strconv.Quote(env.Path),
)
g.Println("")
g.PrintMethodCall("pkg", "SetHash", env.Hash)
g.Println("")
for _, name := range types.Keys() {
t, ok := types.Get(name)
if !ok {
// ke: {"block": {"notest": true}}
continue
}
typ := t.Type.(*system.Type)
isRule := typ.Id.IsRule()
if isRule {
continue
}
interfaceFunc := "nil"
isNativeCollection := typ.IsNativeCollection() && typ.Alias == nil
if !isNativeCollection {
var ifaceName string
if typ.Interface {
ifaceName = system.GoName(typ.Id.Name)
} else {
ifaceName = system.GoInterfaceName(typ.Id.Name)
}
reflectTypeof := g.SprintFunctionCall(
"reflect",
"TypeOf",
fmt.Sprintf("(*%s)(nil)", ifaceName),
) + ".Elem()"
reflectType := builder.Reference("reflect", "Type", env.Path, g.Imports.Add)
interfaceFunc = "func() " + reflectType + " { return " + reflectTypeof + " }"
}
newFunc := "nil"
derefFunc := "nil"
if typ.Interface {
newFunc = "func() interface{} { return (*" + system.GoName(typ.Id.Name) + ")(nil) }"
} else if !typ.IsNativeCollection() || typ.Alias != nil {
newFunc = "func() interface{} { return new(" + system.GoName(typ.Id.Name) + ")}"
if !typ.PassedAsPointer(ctx) {
derefFunc = "func(in interface{}) interface{} {return *in.(*" + system.GoName(typ.Id.Name) + ")}"
}
}
g.Println("pkg.Init(")
{
g.Println(strconv.Quote(typ.Id.Name), ",")
g.Println(newFunc, ",")
g.Println(derefFunc, ",")
g.Println("func() interface{} { return new("+system.GoName(typ.Id.ChangeToRule().Name)+")}", ",")
g.Println(interfaceFunc, ",")
}
g.Println(")")
g.Println("")
}
}
g.Println("}")
}