func generateJavaType(banner string, schema *rdl.Schema, registry rdl.TypeRegistry, outdir string, t *rdl.Type, ns string) error { tName, _, _ := rdl.TypeInfo(t) bt := registry.BaseType(t) switch bt { case rdl.BaseTypeStruct: case rdl.BaseTypeUnion: case rdl.BaseTypeEnum: default: return nil } cName := capitalize(string(tName)) out, file, _, err := outputWriter(outdir, cName, ".java") if err != nil { return err } if file != nil { defer file.Close() } gen := &javaModelGenerator{registry, schema, string(tName), out, nil, ns, true} gen.emitHeader(banner, ns, bt, t) switch bt { case rdl.BaseTypeStruct: gen.emit("\n") gen.emitStruct(t, cName) case rdl.BaseTypeUnion: gen.emit("\n") gen.emitUnion(t) case rdl.BaseTypeArray: gen.emit("\n") gen.emitArray(t) case rdl.BaseTypeEnum: gen.emit("\n") gen.emitTypeComment(t) gen.emitEnum(t) } out.Flush() return gen.err }
func goParamInit(reg rdl.TypeRegistry, qname string, pname string, ptype rdl.TypeRef, pdefault interface{}, poptional bool, precise bool, prefixEnums bool) string { s := "" gtype := goType(reg, ptype, false, "", "", precise, true) switch gtype { /* case "string": if pdefault == nil { s += "\t" + pname + " := rdl.OptionalStringParam(request, \"" + qname + "\")\n" } else { def := fmt.Sprintf("%v", pdefault) s += "\tvar " + pname + "Optional " + gtype + " = " + def + "\n" s += "\t" + pname + ", _ := rdl.StringParam(request, \"" + qname + "\", " + pname + "Optional)\n" } case "bool": if pdefault == nil { s += "\t" + pname + ", err := rdl.OptionalBoolParam(request, \"" + qname + "\")\n" s += "\tif err != nil {\n" s += "\t\trdl.JSONResponse(writer, 400, err)\n" s += "\t\treturn\n" s += "\t}\n" } else { def := fmt.Sprintf("%v", pdefault) s += "\tvar " + pname + "Optional " + gtype + " = " + def + "\n" s += "\t" + pname + " := rdl.BoolParam(request, \"" + qname + "\", " + pname + "Optional)\n" } case "int32", "int64", "int16", "int8": if pdefault == nil { s += "\t" + pname + ", err := rdl.OptionalInt32Param(request, \"" + qname + "\")\n" s += "\tif err != nil {\n\t\trdl.JSONResponse(writer, 400, err)\n\t\treturn\n\t}\n" } else { def := "0" switch v := pdefault.(type) { case float64: def = fmt.Sprintf("%v", v) default: fmt.Println("fix me:", pdefault) panic("fix me") } if precise { s += "\t" + pname + "_, err := rdl.Int32Param(request, \"" + qname + "\", " + def + ")\n" } else { s += "\t" + pname + ", err := rdl.Int32Param(request, \"" + qname + "\", " + def + ")\n" } s += "\tif err != nil {\n\t\trdl.JSONResponse(writer, 400, err)\n\t\treturn\n\t}\n" if precise { s += "\t" + pname + " := " + gtype + "(" + pname + "_)\n" } } */ default: t := reg.FindType(ptype) bt := reg.BaseType(t) switch bt { case rdl.BaseTypeString: if pdefault == nil { if precise && gtype != "string" { s += "\t" + pname + " := " + gtype + "(rdl.OptionalStringParam(request, \"" + qname + "\"))\n" } else { s += "\t" + pname + " := rdl.OptionalStringParam(request, \"" + qname + "\")\n" } } else { def := fmt.Sprintf("%q", pdefault) if precise && gtype != "string" { s += "\t" + pname + "Val, _ := rdl.StringParam(request, \"" + qname + "\", " + def + ")\n" s += "\t" + pname + " := " + gtype + "(" + pname + "Val)\n" } else { s += "\t" + pname + ", _ := rdl.StringParam(request, \"" + qname + "\", " + def + ")\n" } } case rdl.BaseTypeInt32, rdl.BaseTypeInt16, rdl.BaseTypeInt8, rdl.BaseTypeInt64, rdl.BaseTypeFloat32, rdl.BaseTypeFloat64: stype := fmt.Sprint(bt) if pdefault == nil { s += "\t" + pname + ", err := rdl.Optional" + stype + "Param(request, \"" + qname + "\")\n" //! s += "\tif err != nil {\n\t\trdl.JSONResponse(writer, 400, err)\n\t\treturn\n\t}\n" } else { def := "0" switch v := pdefault.(type) { case float64: def = fmt.Sprintf("%v", v) default: fmt.Println("fix me:", pdefault) panic("fix me") } if precise { s += "\t" + pname + "_, err := rdl." + stype + "Param(request, \"" + qname + "\", " + def + ")\n" } else { s += "\t" + pname + ", err := rdl." + stype + "Param(request, \"" + qname + "\", " + def + ")\n" } s += "\tif err != nil {\n\t\trdl.JSONResponse(writer, 400, err)\n\t\treturn\n\t}\n" if precise { s += "\t" + pname + " := " + gtype + "(" + pname + "_)\n" } } case rdl.BaseTypeBool: if pdefault == nil { s += "\t" + pname + ", err := rdl.OptionalBoolParam(request, \"" + qname + "\")\n" s += "\tif err != nil {\n" s += "\t\trdl.JSONResponse(writer, 400, err)\n" s += "\t\treturn\n" s += "\t}\n" } else { def := fmt.Sprintf("%v", pdefault) s += "\tvar " + pname + "Optional " + gtype + " = " + def + "\n" s += "\t" + pname + " := rdl.BoolParam(request, \"" + qname + "\", " + pname + "Optional)\n" } case rdl.BaseTypeEnum: if pdefault == nil { s += fmt.Sprintf("\tvar %s *%s\n", pname, gtype) s += fmt.Sprintf("\t%sOptional := rdl.OptionalStringParam(request, %q)\n", pname, qname) s += fmt.Sprintf("\tif %sOptional != \"\" {\n", pname) s += "\t\tp" + pname + " := New" + gtype + "(" + pname + "Optional)\n" s += "\t\t" + pname + " = &p" + pname + "\n" s += "\t}\n" } else { if prefixEnums { pdefault = gtype + SnakeToCamel(fmt.Sprint(pdefault)) } s += fmt.Sprintf("\t%sOptional, _ := rdl.StringParam(request, %q, %v.String())\n", pname, qname, pdefault) if poptional { s += "\tp" + pname + " := New" + gtype + "(" + pname + "Optional)\n" s += "\t" + pname + " := &p" + pname + "\n" } else { s += "\t" + pname + " := New" + gtype + "(" + pname + "Optional)\n" } } default: fmt.Println("fix me:", pname, "of type", gtype, "with base type", bt) panic("fix me") } } return s }
func goType(reg rdl.TypeRegistry, rdlType rdl.TypeRef, optional bool, items rdl.TypeRef, keys rdl.TypeRef, precise bool, reference bool) string { rdlPrefix := "rdl." if reg.Name() == "rdl" { rdlPrefix = "" } cleanType := string(rdlType) if !strings.HasPrefix(cleanType, "rdl.") { cleanType = capitalize(strings.Replace(string(rdlType), ".", "_", -1)) } prefix := "" if optional { prefix = "*" } t := reg.FindType(rdlType) if t.Variant == 0 { panic("Cannot find type '" + rdlType + "'") } lrdlType := strings.ToLower(string(rdlType)) if precise { switch lrdlType { case "string": return "string" case "symbol": return rdlPrefix + "Symbol" case "bool", "int32", "int64", "int16", "int8", "float64", "float32": return prefix + strings.ToLower(cleanType) default: bt := reg.BaseType(t) switch bt { case rdl.BaseTypeString, rdl.BaseTypeSymbol: return cleanType case rdl.BaseTypeInt8, rdl.BaseTypeInt16, rdl.BaseTypeInt32, rdl.BaseTypeInt64, rdl.BaseTypeFloat32, rdl.BaseTypeFloat64, rdl.BaseTypeBool: return prefix + cleanType case rdl.BaseTypeTimestamp, rdl.BaseTypeUUID: fullTypeName := rdlPrefix + cleanType return prefix + fullTypeName default: if lrdlType == "struct" { fullTypeName := rdlPrefix + cleanType return prefix + fullTypeName } } } } else { switch lrdlType { case "timestamp": return prefix + rdlPrefix + "Timestamp" case "uuid": return prefix + rdlPrefix + "UUID" case "struct": return prefix + rdlPrefix + "Struct" } } bt := reg.BaseType(t) switch bt { case rdl.BaseTypeAny: return "interface{}" case rdl.BaseTypeString: return "string" case rdl.BaseTypeSymbol: return rdlPrefix + "Symbol" case rdl.BaseTypeBool: return prefix + "bool" case rdl.BaseTypeInt8, rdl.BaseTypeInt16, rdl.BaseTypeInt32, rdl.BaseTypeInt64, rdl.BaseTypeFloat32, rdl.BaseTypeFloat64: return prefix + strings.ToLower(bt.String()) case rdl.BaseTypeArray: if reference { name := "Array" if t.ArrayTypeDef != nil { name = string(t.ArrayTypeDef.Name) } if name != "Array" { return name } } i := rdl.TypeRef("Any") switch t.Variant { case rdl.TypeVariantArrayTypeDef: i = t.ArrayTypeDef.Items default: if items != "" { i = items } } gitems := goType(reg, i, false, "", "", precise, reference) return "[]" + gitems case rdl.BaseTypeMap: if reference { //we check if we have defined a type, i.e. the type name is not "Map" name := rdl.TypeName("Map") if t.MapTypeDef != nil { name = t.MapTypeDef.Name } else if t.AliasTypeDef != nil { name = t.AliasTypeDef.Name } if name != "Map" { return string(name) } } k := rdl.TypeRef("Any") i := rdl.TypeRef("Any") switch t.Variant { case rdl.TypeVariantMapTypeDef: k = t.MapTypeDef.Keys i = t.MapTypeDef.Items default: if keys != "" { k = keys } if items != "" { i = items } } gkeys := goType(reg, k, false, "", "", precise, reference) gitems := goType(reg, i, false, "", "", precise, reference) return "map[" + gkeys + "]" + gitems case rdl.BaseTypeStruct: switch t.Variant { case rdl.TypeVariantAliasTypeDef: if t.AliasTypeDef.Name == "Struct" { return prefix + "map[string]interface{}" } } return "*" + cleanType case rdl.BaseTypeUnion: return "*" + cleanType case rdl.BaseTypeEnum: return prefix + cleanType default: return prefix + cleanType } }
func javaType(reg rdl.TypeRegistry, rdlType rdl.TypeRef, optional bool, items rdl.TypeRef, keys rdl.TypeRef) string { t := reg.FindType(rdlType) if t == nil || t.Variant == 0 { panic("Cannot find type '" + rdlType + "'") } bt := reg.BaseType(t) switch bt { case rdl.BaseTypeAny: return "Object" case rdl.BaseTypeString: return "String" case rdl.BaseTypeSymbol, rdl.BaseTypeTimestamp, rdl.BaseTypeUUID: return string(rdlType) case rdl.BaseTypeBool: if optional { return "Boolean" } return "boolean" case rdl.BaseTypeInt8: if optional { return "Byte" } return "byte" case rdl.BaseTypeInt16: if optional { return "Short" } return "short" case rdl.BaseTypeInt32: if optional { return "Integer" } return "int" case rdl.BaseTypeInt64: if optional { return "Long" } return "long" case rdl.BaseTypeFloat32: if optional { return "Float" } return "float" case rdl.BaseTypeFloat64: if optional { return "Double" } return "double" case rdl.BaseTypeArray: i := rdl.TypeRef("Any") switch t.Variant { case rdl.TypeVariantArrayTypeDef: i = t.ArrayTypeDef.Items default: if items != "" && items != "Any" { i = items } } gitems := javaType(reg, rdl.TypeRef(i), false, "", "") //return gitems + "[]" //if arrays, not lists return "List<" + gitems + ">" case rdl.BaseTypeMap: k := rdl.TypeRef("Any") i := rdl.TypeRef("Any") switch t.Variant { case rdl.TypeVariantMapTypeDef: k = t.MapTypeDef.Keys i = t.MapTypeDef.Items default: if keys != "" && keys != "Any" { k = keys } if items != "" && items != "Any" { i = items } } gkeys := javaType(reg, k, false, "", "") gitems := javaType(reg, i, false, "", "") return "Map<" + gkeys + ", " + gitems + ">" case rdl.BaseTypeStruct: if strings.HasPrefix(string(rdlType), "rdl.") { return string(rdlType)[4:] } switch t.Variant { case rdl.TypeVariantStructTypeDef: if t.StructTypeDef.Name == "Struct" { return "Object" } } return string(rdlType) default: return string(rdlType) } }
func makeSwaggerTypeDef(reg rdl.TypeRegistry, t *rdl.Type) *SwaggerType { st := new(SwaggerType) bt := reg.BaseType(t) switch t.Variant { case rdl.TypeVariantStructTypeDef: typedef := t.StructTypeDef st.Description = typedef.Comment props := make(map[string]*SwaggerType) var required []string if len(typedef.Fields) > 0 { for _, f := range typedef.Fields { if f.Optional { required = append(required, string(f.Name)) } ft := reg.FindType(f.Type) fbt := reg.BaseType(ft) prop := new(SwaggerType) prop.Description = f.Comment switch fbt { case rdl.BaseTypeArray: prop.Type = "array" if f.Items != "" { fitems := string(f.Items) items := new(SwaggerType) switch fitems { case "String": items.Type = strings.ToLower(fitems) case "Int32", "Int64", "Int16": items.Type = "integer" items.Format = strings.ToLower(fitems) default: items.Ref = "#/definitions/" + fitems } prop.Items = items } case rdl.BaseTypeString: prop.Type = strings.ToLower(fbt.String()) case rdl.BaseTypeInt32, rdl.BaseTypeInt64, rdl.BaseTypeInt16: prop.Type = "integer" prop.Format = strings.ToLower(fbt.String()) case rdl.BaseTypeStruct: prop.Type = "#/definitions/" + string(f.Type) case rdl.BaseTypeMap: prop.Type = "object" if f.Items != "" { fitems := string(f.Items) items := new(SwaggerType) switch f.Items { case "String": items.Type = strings.ToLower(fitems) case "Int32", "Int64", "Int16": items.Type = "integer" items.Format = strings.ToLower(fitems) default: items.Ref = "#/definitions/" + fitems } prop.AdditionalProperties = items } default: prop.Type = "_" + string(f.Type) + "_" //! } props[string(f.Name)] = prop } } st.Properties = props if len(required) > 0 { st.Required = required } case rdl.TypeVariantArrayTypeDef: typedef := t.ArrayTypeDef st.Type = bt.String() if typedef.Items != "Any" { items := new(SwaggerType) switch reg.FindBaseType(typedef.Items) { case rdl.BaseTypeString: items.Type = strings.ToLower(string(typedef.Items)) case rdl.BaseTypeInt32, rdl.BaseTypeInt64, rdl.BaseTypeInt16: items.Type = "integer" items.Format = strings.ToLower(string(typedef.Items)) default: items.Ref = "#/definitions/" + string(typedef.Items) } st.Items = items } case rdl.TypeVariantEnumTypeDef: typedef := t.EnumTypeDef var tmp []string for _, el := range typedef.Elements { tmp = append(tmp, string(el.Symbol)) } st.Enum = tmp case rdl.TypeVariantUnionTypeDef: typedef := t.UnionTypeDef fmt.Println("[" + typedef.Name + ": Swagger doesn't support unions]") default: switch bt { case rdl.BaseTypeString, rdl.BaseTypeInt16, rdl.BaseTypeInt32, rdl.BaseTypeInt64, rdl.BaseTypeFloat32, rdl.BaseTypeFloat64: return nil default: panic(fmt.Sprintf("whoops: %v", t)) } } return st }