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 + ";"
}
Exemple #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)
}
Exemple #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
}
Exemple #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
}
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 + ";"
}
Exemple #6
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
}
Exemple #7
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)
	}
}
Exemple #8
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
	}
}
Exemple #9
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
}