本文整理汇总了Golang中limbo/services/protobuf/protoc-gen-gogo/generator.FileDescriptor.Enums方法的典型用法代码示例。如果您正苦于以下问题:Golang FileDescriptor.Enums方法的具体用法?Golang FileDescriptor.Enums怎么用?Golang FileDescriptor.Enums使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类limbo/services/protobuf/protoc-gen-gogo/generator.FileDescriptor
的用法示例。
在下文中一共展示了FileDescriptor.Enums方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Generate
func (p *enumstringer) Generate(file *generator.FileDescriptor) {
p.PluginImports = generator.NewPluginImports(p.Generator)
p.atleastOne = false
p.localName = generator.FileName(file)
strconvPkg := p.NewImport("strconv")
for _, enum := range file.Enums() {
if !gogoproto.IsEnumStringer(file.FileDescriptorProto, enum.EnumDescriptorProto) {
continue
}
if gogoproto.IsGoEnumStringer(file.FileDescriptorProto, enum.EnumDescriptorProto) {
panic("old enum string method needs to be disabled, please use gogoproto.old_enum_stringer or gogoproto.old_enum_string_all and set it to false")
}
p.atleastOne = true
ccTypeName := generator.CamelCaseSlice(enum.TypeName())
p.P("func (x ", ccTypeName, ") String() string {")
p.In()
p.P(`s, ok := `, ccTypeName, `_name[int32(x)]`)
p.P(`if ok {`)
p.In()
p.P(`return s`)
p.Out()
p.P(`}`)
p.P(`return `, strconvPkg.Use(), `.Itoa(int(x))`)
p.Out()
p.P(`}`)
}
if !p.atleastOne {
return
}
}
示例2: Generate
// Generate generates code for the services in the given file.
func (g *jsonschema) Generate(file *generator.FileDescriptor) {
imp := generator.NewPluginImports(g.gen)
g.imports = imp
g.runtimePkg = imp.NewImport("limbo.services/core/runtime/limbo")
g.operations = nil
g.definitions = map[string]interface{}{}
g.dependencies = map[string][]string{}
for i, enum := range file.Enums() {
g.generateEnumSchema(file, enum, i)
}
for i, message := range file.Messages() {
g.generateMessageSchema(file, message, i)
}
for i, service := range file.Service {
g.generateServiceSchema(file, service, i)
}
if len(g.definitions) > 0 {
defVarName := fmt.Sprintf("jsonSchemaDefs%x", sha1.Sum([]byte(file.GetName())))
g.gen.AddInitf(`%s.RegisterSchemaDefinitions(%s)`, g.runtimePkg.Use(), defVarName)
g.P(`var `+defVarName+` = []`, g.runtimePkg.Use(), `.SchemaDefinition{`)
var definitionNames = make([]string, 0, len(g.definitions))
for name := range g.definitions {
definitionNames = append(definitionNames, name)
}
sort.Strings(definitionNames)
for _, name := range definitionNames {
def := g.definitions[name]
data, err := json.MarshalIndent(def, "\t\t", "\t")
if err != nil {
panic(err)
}
dataStr := strings.Replace(string(data), "`", "`+\"`\"+`", -1)
g.P(`{`)
g.P(`Name: `, strconv.Quote(name), `,`)
g.P(`Dependencies: []string{`)
for _, dep := range g.dependencies[name] {
g.P(strconv.Quote(dep), ",")
}
g.P(`},`)
g.P(`Definition: []byte(`, "`", dataStr, "`),")
g.P(`},`)
}
g.P(`}`)
}
if len(g.operations) > 0 {
defVarName := fmt.Sprintf("swaggerDefs%x", sha1.Sum([]byte(file.GetName())))
g.gen.AddInitf(`%s.RegisterSwaggerOperations(%s)`, g.runtimePkg.Use(), defVarName)
g.P(`var `+defVarName+` = []`, g.runtimePkg.Use(), `.SwaggerOperation{`)
for _, op := range g.operations {
data, err := json.MarshalIndent(op.Swagger, "\t\t", "\t")
if err != nil {
panic(err)
}
dataStr := strings.Replace(string(data), "`", "`+\"`\"+`", -1)
g.P(`{`)
g.P(`Pattern: `, strconv.Quote(op.Pattern), `,`)
g.P(`Method: `, strconv.Quote(op.Method), `,`)
g.P(`Dependencies: []string{`)
for _, dep := range op.Dependencies {
g.P(strconv.Quote(dep), ",")
}
g.P(`},`)
g.P(`Definition: []byte(`, "`", dataStr, "`),")
g.P(`},`)
}
g.P(`}`)
}
}