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
	}

}
Exemple #2
0
// 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(`}`)
	}
}