Beispiel #1
0
// ParseType processes a type string and returns the IDL type for it
func ParseType(s string) (*idl.Type, error) {
	var err error
	var t idl.Type
	var m []string
	if m = mapRE.FindStringSubmatch(s); m != nil {
		i := findSplit(m[1])
		if i < 0 {
			return nil, errors.New("Unable to break key/value: " + s)
		}
		t.Name = "map"
		t.KeyType, err = ParseType(m[1][:i])
		if err != nil {
			return nil, err
		}
		if !t.KeyType.IsPrimitive() {
			return nil, errors.New("Key type must be a primitive type: " + s)
		}
		t.ValueType, err = ParseType(m[1][i+1:])
		if err != nil {
			return nil, err
		}
	} else if m = listRE.FindStringSubmatch(s); m != nil {
		t.Name = "list"
		t.ValueType, err = ParseType(m[1])
		if err != nil {
			return nil, err
		}
	} else if m = basicRE.FindStringSubmatch(s); m != nil {
		t.Name = m[1]
	} else {
		return nil, errors.New("Uanble to parse type: " + s)
	}
	return &t, nil
}
Beispiel #2
0
// getTypeKey return the correct JSON key for the specific type, ie type,ref,or enumRef
func (gen *testGenerator) getTypeKey(t *idl.Type) string {
	var s string
	if t.IsEnum(gen.tplRootIdl) {
		s = "enumRef"
	} else if t.IsStruct(gen.tplRootIdl) {
		s = "ref"
	} else {
		s = "type"
	}
	return s
}
Beispiel #3
0
func returnsToSchema(pidl *idl.Idl, t *idl.Type) *swagger2.Schema {
	sc := new(swagger2.Schema)
	// sc.Title = f.Name
	if t.IsVoid() {
		// nil schema means the operation returns no content
		return nil
	} else {
		it := typeToItems(pidl, t)
		sc.Ref = it.Ref
		sc.Type = it.Type
		sc.Format = it.Format
		sc.ItemsDef.Items = it.Items
		sc.Enum = it.Enum
		sc.AdditionalProperties = it.AdditionalProperties
	}
	return sc
}
Beispiel #4
0
// formatType returns the Type as a string in format suitable for C#.
func (gen *csharpGenerator) formatType(t *idl.Type) string {
	var s string
	ms, ok := csharpTypes[t.Name]
	if !ok {
		ms = t.Name
	}
	if t.Name == "list" {
		s = fmt.Sprintf(ms, gen.formatType(t.ValueType))
	} else if t.Name == "map" {
		s = fmt.Sprintf(ms, csharpTypes[t.KeyType.Name], gen.formatType(t.ValueType))
	} else if t.IsPrimitive() && t.Name != "string" {
		s = ms + "?"
	} else if t.IsEnum(gen.tplRootIdl) {
		s = ms + "?"
	} else {
		s = ms
	}
	return s
}
Beispiel #5
0
// fullTypeName returns the fully qualified type name using namespace syntax for go.
// Note: this probably doesn't work.
func (gen *goGenerator) fullTypeName(t *idl.Type) string {
	var s string
	ms, ok := goTypes[t.Name]
	if !ok {
		ms = t.Name
	}
	if t.Name == "list" {
		s = fmt.Sprintf(ms, gen.fullTypeName(t.ValueType))
	} else if t.Name == "map" {
		s = fmt.Sprintf(ms, goTypes[t.KeyType.Name], gen.fullTypeName(t.ValueType))
	} else if t.IsPrimitive() {
		s = "*" + ms
	} else {
		ns := gen.tplRootIdl.NamespaceOf(ms, "go")
		if ns != "" {
			s = fmt.Sprintf("*%s.%s", ns, ms)
		} else {
			s = ms
		}
	}
	return s
}
Beispiel #6
0
func (gen *javaGenerator) isVoid(t *idl.Type) bool {
	return t.IsVoid()
}
Beispiel #7
0
func toType(val []string, midl *idl.Idl, typ *idl.Type, fmt rest.ListFmt) (interface{}, error) {
	if typ.IsBool() {
		return strconv.ParseBool(one(val))
	} else if typ.IsInt() {
		if typ.Name == "int64" {
			// int64 is quoted
			return one(val), nil
		} else {
			return strconv.ParseInt(one(val), 10, 32)
		}
	} else if typ.IsString() || typ.IsChar() || typ.IsDatetime() || typ.IsBinary() || typ.IsDecimal() {
		// treat as string - these are all quoted
		return one(val), nil
	} else if typ.IsFloat() {
		return strconv.ParseFloat(one(val), 64)
	} else if typ.IsList() && typ.ValueType.IsPrimitive() {
		var sep string
		switch fmt {
		case rest.CSV:
			sep = ","
		case rest.SSV:
			sep = " "
		case rest.TSV:
			sep = "\t"
		case rest.PIPES:
			sep = "|"
		case rest.MULTI:
		default:
			return nil, errors.New("Format must be specified when lists are encoded.")
		}
		vals := make([]interface{}, 0)
		var arr []string
		if sep != "" {
			arr = strings.Split(one(val), sep)
		} else {
			if val == nil {
				arr = []string{}
			} else {
				arr = val
			}
		}
		for _, s := range arr {
			v, err := toType([]string{s}, midl, typ.ValueType, rest.NONE)
			if err != nil {
				return nil, err
			}
			vals = append(vals, v)
		}
		return vals, nil
	} else if typ.IsEnum(midl) {
		// treat as string - quoted
		return one(val), nil
	}

	// all other types are custom
	// in theory maybe maps could be handled?
	return nil, errors.New("Unexpected type: " + typ.String())
}
Beispiel #8
0
func typeToItems(pidl *idl.Idl, t *idl.Type) *swagger2.ItemsDef {
	it := new(swagger2.ItemsDef)
	it.Ref = ""
	if t.IsPrimitive() {
		it.Format = t.String()
		if t.IsInt() || t.IsByte() {
			it.Type = "integer"
			it.Format = "int32"
			if t.Name == "int64" {
				if swagInt && restful {
					// Swagger style int64
					it.Format = "int64"
				} else {
					// Babel style int64
					it.Type = "string"  // ??? Babel quotes large integers to avoid precision loss in JavaScript
					it.Format = "int64" // SWAGGER-CLARIFICATION: is format int64 legal with type string?
				}
			}
		} else if t.IsFloat() {
			it.Type = "number"
			it.Format = "float"
			if t.Name == "float64" {
				it.Format = "double"
			}
		} else if t.IsBool() {
			it.Type = "boolean"
			it.Format = ""
		} else if t.IsDatetime() {
			it.Type = "string"
			it.Format = "date-time"
		} else if t.IsDecimal() {
			it.Type = "string"
			it.Format = ""
		} else if t.IsString() || t.IsChar() {
			it.Type = "string"
			it.Format = ""
		}
	} else if t.IsBinary() {
		it.Type = "string"
		it.Format = "byte"
	} else if t.IsMap() {
		it.Type = "object"
		// hmmm....what to do if keytype is not string?
		// SWAGGER-CLARIFICATION: Does swagger require all key types to be strings?
		it.AdditionalProperties = typeToItems(pidl, t.ValueType)
	} else if t.IsList() {
		it.Type = "array"
		it.Format = ""
		it.Items = typeToItems(pidl, t.ValueType)
	} else if t.IsEnum(pidl) {
		// SWAGGER-BUG: Enums cannot be delared in a schema
		it.Type = "string"
		it.Format = ""
		it.Enum = make([]interface{}, 0)
		e := pidl.FindEnum(t.Name)
		if e != nil {
			for _, x := range e.Values {
				it.Enum = append(it.Enum, x.Name)
			}
		}
	} else {
		// user-defined, struct or enum
		it.Ref = "#/definitions/" + t.Name
	}
	return it
}
Beispiel #9
0
// formatType returns the Type as a string in format suitable for C#.
func (gen *goGenerator) formatType(t *idl.Type) string {
	var s string
	ms, ok := goTypes[t.Name]
	if !ok {
		ms = t.Name
	}
	if t.IsList() {
		s = fmt.Sprintf(ms, gen.formatType(t.ValueType))
	} else if t.IsMap() {
		s = fmt.Sprintf(ms, goTypes[t.KeyType.Name], gen.formatType(t.ValueType))
	} else if t.IsBinary() {
		s = ms
	} else if t.IsPrimitive() {
		s = "*" + ms
	} else if t.IsEnum(gen.tplRootIdl) {
		s = "*" + ms
	} else if t.IsVoid() {
		s = ""
	} else {
		s = "*" + ms
	}
	return s
}
Beispiel #10
0
func (gen *csharpGenerator) isTrivialProperty(t *idl.Type) bool {
	return t.IsPrimitive() || t.Name == "binary" || t.IsEnum(gen.tplRootIdl)
}
Beispiel #11
0
func (gen *csharpGenerator) isVoid(t *idl.Type) bool {
	return t.IsVoid()
}