func (g *generateServiceBuilder) buildFunction(spec *compile.FunctionSpec) (*api.Function, error) { args, err := g.buildFieldGroup(compile.FieldGroup(spec.ArgsSpec)) if err != nil { return nil, err } function := &api.Function{ Name: goCase(spec.Name), ThriftName: spec.Name, Arguments: args, } if spec.OneWay { function.OneWay = ptr.Bool(spec.OneWay) } if spec.ResultSpec != nil { var err error result := spec.ResultSpec if result.ReturnType != nil { function.ReturnType, err = g.buildType(result.ReturnType, true) if err != nil { return nil, err } } if len(result.Exceptions) > 0 { function.Exceptions, err = g.buildFieldGroup(result.Exceptions) if err != nil { return nil, err } } } return function, nil }
// ServiceFunction generates code for the given function of the given service. func ServiceFunction(g Generator, s *compile.ServiceSpec, f *compile.FunctionSpec) error { argsGen := fieldGroupGenerator{ Namespace: NewNamespace(), Name: functionNamePrefix(s, f) + "Args", Fields: compile.FieldGroup(f.ArgsSpec), } if err := argsGen.Generate(g); err != nil { return wrapGenerateError(fmt.Sprintf("%s.%s", s.Name, f.Name), err) } if err := functionArgsEnveloper(g, s, f); err != nil { return wrapGenerateError(fmt.Sprintf("%s.%s", s.Name, f.Name), err) } if err := functionHelper(g, s, f); err != nil { return wrapGenerateError(fmt.Sprintf("%s.%s", s.Name, f.Name), err) } if f.ResultSpec == nil { return nil } resultFields := make(compile.FieldGroup, 0, len(f.ResultSpec.Exceptions)+1) if f.ResultSpec.ReturnType != nil { resultFields = append(resultFields, &compile.FieldSpec{ ID: 0, Name: "success", Type: f.ResultSpec.ReturnType, }) } resultFields = append(resultFields, f.ResultSpec.Exceptions...) resultGen := fieldGroupGenerator{ Namespace: NewNamespace(), Name: functionNamePrefix(s, f) + "Result", Fields: resultFields, IsUnion: true, AllowEmptyUnion: f.ResultSpec.ReturnType == nil, } if err := resultGen.Generate(g); err != nil { return wrapGenerateError(fmt.Sprintf("%s.%s", s.Name, f.Name), err) } if err := functionResponseEnveloper(g, s, f); err != nil { return wrapGenerateError(fmt.Sprintf("%s.%s", s.Name, f.Name), err) } // TODO(abg): If we receive unknown exceptions over the wire, we need to // throw a generic error. return nil }