func NewMethod(r *raml.Resource, rd *Resource, m *raml.Method, methodName string, sbn SetBodyName) Method { method := Method{ Method: m, Endpoint: r.FullURI(), verb: strings.ToUpper(methodName), RAMLResource: r, } // set request body method.ReqBody = sbn(m.Bodies, method.Endpoint+methodName, commons.ReqBodySuffix) //set response body for k, v := range m.Responses { code := commons.AtoiOrPanic(string(k)) if code >= 200 && code < 300 { method.RespBody = sbn(v.Bodies, method.Endpoint+methodName, commons.RespBodySuffix) } } // set func comment if len(m.Description) > 0 { method.FuncComments = commons.ParseDescription(m.Description) } return method }
// create a python class representations func newClass(name, description string, properties map[string]interface{}) class { pc := class{ Name: name, Description: commons.ParseDescription(description), Fields: map[string]field{}, } // generate fields for k, v := range properties { p := raml.ToProperty(k, v) field := field{ Name: p.Name, Required: p.Required, } field.setType(p.Type) if field.Type == "" { // type is not supported, no need to generate the field continue } field.buildValidators(p) pc.Fields[p.Name] = field } return pc }
// create new struct def func newStructDef(name, packageName, description string, properties map[string]interface{}) structDef { // generate struct's fields from type properties fields := make(map[string]fieldDef) for k, v := range properties { prop := raml.ToProperty(k, v) fd := fieldDef{ Name: strings.Title(prop.Name), Type: convertToGoType(prop.Type), IsOmitted: !prop.Required, } fd.buildValidators(prop) fields[prop.Name] = fd } return structDef{ Name: name, PackageName: packageName, Fields: fields, Description: commons.ParseDescription(description), } }
func newObject(name, description string, properties map[string]interface{}) (object, error) { // generate fields from type properties fields := make(map[string]field) for k, v := range properties { prop := raml.ToProperty(k, v) fd := field{ Name: prop.Name, Type: toNimType(prop.Type), } if fd.Type == "" { return object{}, fmt.Errorf("unsupported type in nim:%v", prop.Type) } fields[prop.Name] = fd } return object{ Name: name, Fields: fields, Description: commons.ParseDescription(description), }, nil }
func NewStruct(t raml.Type, name, lang, pkg string) (Struct, error) { // generate fields from type properties fields := make(map[string]field) for k, v := range t.Properties { fd := newField(name, raml.ToProperty(k, v), lang, pkg) fields[fd.Name] = fd } s := Struct{ ID: getID(), Name: name, Fields: fields, Description: commons.ParseDescription(t.Description), T: t, pkg: pkg, lang: lang, } if err := s.checkValidCapnp(); err != nil { return s, err } return s, s.orderFields() }