예제 #1
0
func javaGenerateStructTypeConstructor(reg rdl.TypeRegistry, t *rdl.StructTypeDef) string {
	s := fmt.Sprintf("    sb.structType(%q", t.Name)
	if t.Type != "Struct" {
		s += fmt.Sprintf(", %q)", t.Type)
	} else {
		s += ")"
	}
	if t.Comment != "" {
		s += fmt.Sprintf("\n            .comment(%q)", t.Comment)
	}
	for _, f := range t.Fields {
		if f.Keys != "" {
			fkeys := string(f.Keys)   //javaType(reg, f.Keys, false, "", "")
			fitems := string(f.Items) //javaType(reg, f.Items, false, "", "")
			s += fmt.Sprintf("\n            .mapField(%q, %q, %q, %v, %q)", f.Name, fkeys, fitems, f.Optional, f.Comment)
		} else if f.Items != "" {
			fitems := string(f.Items) //javaType(reg, f.Items, false, "", "")
			s += fmt.Sprintf("\n            .arrayField(%q, %q, %v, %q)", f.Name, fitems, f.Optional, f.Comment)
		} else {
			ftype := string(f.Type) //javaType(reg, f.Type, f.Optional, "", "")
			if f.Default != nil {
				ft := reg.FindType(f.Type)
				ss := "null"
				if ft != nil {
					ss = javaLiteral(ft, f.Default)
				}
				s += fmt.Sprintf("\n            .field(%q, %q, %v, %q, %s)", f.Name, ftype, false, f.Comment, ss)
			} else {
				s += fmt.Sprintf("\n            .field(%q, %q, %v, %q)", f.Name, ftype, f.Optional, f.Comment)
			}
		}
	}
	return s + ";"
}
예제 #2
0
func annotate(registry rdl.TypeRegistry, typename rdl.TypeRef) string {
	t := registry.FindType(typename)
	if t != nil {
		tName, tType, _ := rdl.TypeInfo(t)
		if tType != rdl.TypeRef(tName) {
			return "[" + string(typename) + "](#" + strings.ToLower(string(typename)) + ")"
		}
	}
	return string(typename)
}
예제 #3
0
func typeStack(registry rdl.TypeRegistry, typeDef *rdl.Type) []*rdl.Type {
	var types []*rdl.Type
	types = append(types, typeDef)
	tName, tType, _ := rdl.TypeInfo(typeDef)
	for tName != rdl.TypeName(tType) {
		supertype := registry.FindType(tType)
		types = append(types, supertype)
		tName, tType, _ = rdl.TypeInfo(supertype)
	}
	return types
}
예제 #4
0
func addFields(reg rdl.TypeRegistry, dst []*rdl.StructFieldDef, t *rdl.Type) []*rdl.StructFieldDef {
	switch t.Variant {
	case rdl.TypeVariantStructTypeDef:
		st := t.StructTypeDef
		if st.Type != "Struct" {
			dst = addFields(reg, dst, reg.FindType(st.Type))
		}
		for _, f := range st.Fields {
			dst = append(dst, f)
		}
	}
	return dst
}
예제 #5
0
func explodeURL(reg rdl.TypeRegistry, r *rdl.Resource) string {
	path := r.Path
	params := ""
	delim := ""
	for _, v := range r.Inputs {
		k := v.Name
		gk := goName(string(k))
		if v.PathParam {
			//
			if v.Type == "String" {
				path = strings.Replace(path, "{"+string(k)+"}", "\" + "+gk+" + \"", -1)
			} else {
				path = strings.Replace(path, "{"+string(k)+"}", "\" + fmt.Sprint("+gk+") + \"", -1)
			}
		} else if v.QueryParam != "" {
			qp := v.QueryParam
			item := ""
			if reg.IsArrayTypeName(v.Type) {
				item = "encodeListParam(\"" + qp + "\"," + gk + ")"
			} else {
				baseType := reg.BaseTypeName(v.Type)
				if v.Optional && baseType != "String" {
					item = "encodeOptional" + string(baseType) + "Param(\"" + qp + "\", " + gk + ")"
				} else {
					def := goLiteral(v.Default, string(baseType))
					if baseType == "Enum" {
						def = "\"" + def + "\""
						item = "encodeStringParam(\"" + qp + "\", " + gk + ".String(), " + def + ")"
					} else {
						item = "encode" + string(baseType) + "Param(\"" + qp + "\", " + strings.ToLower(string(baseType)) + "(" + gk + "), " + def + ")"
					}
				}
			}
			params += delim + item
			delim = ", "
		}
	}
	path = "\"" + path
	if strings.HasSuffix(path, " + \"") {
		path = path[0 : len(path)-4]
	} else {
		path += "\""
	}
	if params != "" {
		path = path + " + encodeParams(" + params + ")"
	}
	return path
}
예제 #6
0
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
}
예제 #7
0
func makeSwaggerTypeRef(reg rdl.TypeRegistry, itemTypeName rdl.TypeRef) (string, string, *SwaggerType) {
	itype := string(itemTypeName)
	switch reg.FindBaseType(itemTypeName) {
	case rdl.BaseTypeInt8:
		return "string", "byte", nil
	case rdl.BaseTypeInt16, rdl.BaseTypeInt32, rdl.BaseTypeInt64:
		return "integer", strings.ToLower(itype), nil
	case rdl.BaseTypeFloat32:
		return "number", "float", nil
	case rdl.BaseTypeFloat64:
		return "number", "double", nil
	case rdl.BaseTypeString:
		return "string", "", nil
	case rdl.BaseTypeTimestamp:
		return "string", "date-time", nil
	case rdl.BaseTypeUUID, rdl.BaseTypeSymbol:
		return "string", strings.ToLower(itype), nil
	default:
		s := new(SwaggerType)
		s.Ref = "#/definitions/" + itype
		return "", "", s
	}
}
예제 #8
0
func javaGenerateResourceConstructor(reg rdl.TypeRegistry, rez *rdl.Resource) string {
	rTypeName := rez.Type
	if rez.Method == "PUT" || rez.Method == "POST" {
		for _, ri := range rez.Inputs {
			if !ri.PathParam && ri.QueryParam == "" && ri.Header == "" {
				rTypeName = ri.Type
				break
			}
		}
	}
	s := fmt.Sprintf("    sb.resource(%q, %q, %q)", rTypeName, rez.Method, rez.Path)
	if rez.Comment != "" {
		s += fmt.Sprintf("\n            .comment(%q)", rez.Comment)
	}
	for _, ri := range rez.Inputs {
		def := "null"
		if ri.Default != nil {
			ft := reg.FindType(ri.Type)
			if ft != nil {
				def = javaLiteral(ft, ri.Default)
			}
		}
		if ri.PathParam {
			s += fmt.Sprintf("\n            .pathParam(%q, %q, %q)", ri.Name, ri.Type, ri.Comment)
		} else if ri.QueryParam != "" {
			s += fmt.Sprintf("\n            .queryParam(%q, %q, %q, %s, %q)", ri.QueryParam, ri.Name, ri.Type, def, ri.Comment)
		} else if ri.Header != "" {
			s += fmt.Sprintf("\n            .headerParam(%q, %q, %q, %s, %q)", ri.Header, ri.Name, ri.Type, def, ri.Comment)
		} else {
			s += fmt.Sprintf("\n            .input(%q, %q, %q)", ri.Name, ri.Type, ri.Comment)
		}
	}
	for _, ro := range rez.Outputs {
		s += fmt.Sprintf("\n            .output(%q, %q, %q, %q)", ro.Header, ro.Name, ro.Type, ro.Comment)
	}
	if rez.Auth != nil {
		if rez.Auth.Domain != "" {
			s += fmt.Sprintf("\n            .auth(%q, %q, %v, %q)", rez.Auth.Action, rez.Auth.Resource, rez.Auth.Authenticate, rez.Auth.Domain)
		} else if rez.Auth.Authenticate {
			s += fmt.Sprintf("\n            .auth(%q, %q, true)", rez.Auth.Action, rez.Auth.Resource)
		} else {
			s += fmt.Sprintf("\n            .auth(%q, %q)", rez.Auth.Action, rez.Auth.Resource)
		}
	}
	s += fmt.Sprintf("\n            .expected(%q)", rez.Expected)
	//build a sorted order for the exceptions, to make them predictable. Go randomizes the order otherwise.
	var syms []string
	for sym, _ := range rez.Exceptions {
		syms = append(syms, sym)
	}
	sort.Strings(syms)
	for _, sym := range syms {
		re := rez.Exceptions[sym]
		s += fmt.Sprintf("\n            .exception(%q, %q, %q)\n", sym, re.Type, re.Comment)
	}
	if rez.Async != nil {
		if *rez.Async {
			s += fmt.Sprintf("\n            .async()\n")
		}
	}
	return s + ";"
}
예제 #9
0
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
}
예제 #10
0
func goHandlerBody(reg rdl.TypeRegistry, name string, r *rdl.Resource, precise bool, prefixEnums bool) string {
	s := ""
	var fargs []string
	bodyName := ""
	for _, in := range r.Inputs {
		name := "arg" + capitalize(string(in.Name))
		if in.QueryParam != "" {
			qname := in.QueryParam
			if in.Optional || in.Default != nil {
				s += goParamInit(reg, qname, name, in.Type, in.Default, in.Optional, precise, prefixEnums)
			} else {
				log.Println("RDL error: queryparam must either be optional or have a default value")
			}
			fargs = append(fargs, name)
		} else if in.PathParam {
			bt := reg.BaseTypeName(in.Type)
			switch bt {
			case "Enum":
				s += fmt.Sprintf("\t%s := New%s(context.Params[%q])\n", name, in.Type, in.Name)
			case "Int32", "Int64", "Int16", "Int8":
				if precise {
					s += fmt.Sprintf("\t%s := %s(intFromString(context.Params[%q]))\n", name, in.Type, in.Name)
				} else {
					s += fmt.Sprintf("\t%s := intFromString(context.Params[%q])\n", name, in.Name)
				}
			case "Float32", "Float64":
				if precise {
					s += fmt.Sprintf("\t%s := %s(floatFromString(context.Params[%q]))\n", name, in.Type, in.Name)
				} else {
					s += fmt.Sprintf("\t%s := floatFromString(context.Params[%q])\n", name, in.Name)
				}
			default:
				if precise && strings.ToLower(string(in.Type)) != "string" {
					s += fmt.Sprintf("\t%s := %s(context.Params[%q])\n", name, in.Type, in.Name)
				} else {
					s += fmt.Sprintf("\t%s := context.Params[%q]\n", name, in.Name)
				}
			}
			fargs = append(fargs, name)
		} else if in.Header != "" {
			hname := in.Header
			def := ""
			if in.Default != nil {
				switch v := in.Default.(type) {
				case string:
					def = fmt.Sprintf("%q", v)
				default:
					panic(fmt.Sprintf("implement me, default value: %v", in))
				}
				s += "\t" + name + "Optional := " + def + "\n"
				s += fmt.Sprintf("\t%s := rdl.HeaderParam(request, %q, %sOptional)\n", name, hname, name)
			} else if in.Optional {
				s += fmt.Sprintf("\t%s := rdl.OptionalHeaderParam(request, %q)\n", name, hname)
			} else {
				s += fmt.Sprintf("\t%s := rdl.HeaderParam(request, %q, \"\")\n", name, hname)
			}
			fargs = append(fargs, name)
		} else {
			bodyName = name
			s += "\tbody, oserr := ioutil.ReadAll(request.Body)\n"
			s += "\tif oserr != nil {\n"
			s += "\t\trdl.JSONResponse(writer, http.StatusBadRequest, rdl.ResourceError{Code: http.StatusBadRequest, Message: \"Bad request: \" + oserr.Error()})\n"
			s += "\t\treturn\n"
			s += "\t}\n"
			pgtype := goType(reg, in.Type, false, "", "", precise, true)
			s += "\tvar " + bodyName + " " + pgtype + "\n"
			s += "\toserr = json.Unmarshal(body, &" + bodyName + ")\n"
			s += "\tif oserr != nil {\n"
			s += "\t\trdl.JSONResponse(writer, http.StatusBadRequest, rdl.ResourceError{Code: http.StatusBadRequest, Message: \"Bad request: \" + oserr.Error()})\n"
			s += "\t\treturn\n"
			s += "\t}\n"
			fargs = append(fargs, bodyName)
		}
	}
	if r.Auth != nil {
		if r.Auth.Authenticate {
			s += authenticateTemplate
		} else if r.Auth.Action != "" && r.Auth.Resource != "" {
			resource := r.Auth.Resource
			i := strings.Index(resource, "{")
			for i >= 0 {
				j := strings.Index(resource[i:], "}")
				if j < 0 {
					break
				}
				j += i
				val := "string(arg" + capitalize(resource[i+1:j]) + ")"
				resource = resource[0:i] + "\" + " + val + " + \"" + resource[j+1:]
				i = strings.Index(resource, "{")
			}
			resource = "\"" + resource
			if strings.HasSuffix(resource, "+ \"") {
				resource = resource[0 : len(resource)-3]
			} else {
				resource = resource + "\""
			}
			if strings.HasPrefix(resource, "\"\" + ") {
				resource = resource[5:]
			}
			s += fmt.Sprintf(authorizeTemplate, r.Auth.Action, resource)
		} else {
			log.Println("*** Badly formed auth spec in resource input:", r)
		}
	}
	methName, _ := goMethodName(reg, r, precise)
	sargs := ""
	if len(fargs) > 0 {
		sargs = ", " + strings.Join(fargs, ", ")
	}
	outHeaders := ""
	for _, v := range r.Outputs {
		outHeaders += ", " + string(v.Name)
	}
	noContent := r.Expected == "NO_CONTENT" && len(r.Alternatives) == 0
	if noContent {
		s += "\terr" + outHeaders + " := adaptor.impl." + capitalize(methName) + "(context" + sargs + ")\n"
	} else {
		s += "\tdata" + outHeaders + ", err := adaptor.impl." + capitalize(methName) + "(context" + sargs + ")\n"
	}
	s += "\tif err != nil {\n"
	s += "\t\tswitch e := err.(type) {\n"
	s += "\t\tcase *rdl.ResourceError:\n"
	//special case the 304 response, which MUST have an etag in it
	for _, v := range r.Outputs {
		if strings.ToLower(v.Header) == "etag" {
			s += "\t\t\tif e.Code == 304 && " + string(v.Name) + " != \"\" {\n"
			s += "\t\t\t\twriter.Header().Set(\"" + v.Header + "\", " + string(v.Name) + ")\n"
			s += "\t\t\t}\n"
			break
		}
	}

	s += "\t\t\trdl.JSONResponse(writer, e.Code, err)\n"
	s += "\t\tdefault:\n"
	s += "\t\t\trdl.JSONResponse(writer, 500, &rdl.ResourceError{Code: 500, Message: e.Error()})\n"
	s += "\t\t}\n"
	s += "\t} else {\n"
	for _, v := range r.Outputs {
		vname := string(v.Name)
		if v.Optional {
			s += "\t\tif " + vname + " != nil {\n"
			s += "\t\t\twriter.Header().Set(\"" + v.Header + "\", " + vname + ")\n"
			s += "\t\t}\n"
		} else {
			s += "\t\twriter.Header().Set(\"" + v.Header + "\", " + vname + ")\n"
		}
	}
	if noContent { //other non-content responses?
		s += fmt.Sprintf("\t\twriter.WriteHeader(204)\n")
	} else {
		//fixme: handle alternative responses. How deos the handler pass them back?
		s += fmt.Sprintf("\t\trdl.JSONResponse(writer, %s, data)\n", rdl.StatusCode(r.Expected))
	}
	s += "\t}\n"
	return s
}
예제 #11
0
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)
	}
}
예제 #12
0
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
	}
}
예제 #13
0
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
}