Beispiel #1
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
	}
}
Beispiel #2
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
}