Пример #1
0
// TypeCode gets an expression of type 'wire.Type' that represents the
// over-the-wire type code for the given TypeSpec.
func TypeCode(g Generator, spec compile.TypeSpec) string {
	wire := g.Import("go.uber.org/thriftrw/wire")
	spec = compile.RootTypeSpec(spec)

	switch spec.(type) {
	case *compile.BoolSpec:
		return fmt.Sprintf("%s.TBool", wire)
	case *compile.I8Spec:
		return fmt.Sprintf("%s.TI8", wire)
	case *compile.I16Spec:
		return fmt.Sprintf("%s.TI16", wire)
	case *compile.I32Spec:
		return fmt.Sprintf("%s.TI32", wire)
	case *compile.I64Spec:
		return fmt.Sprintf("%s.TI64", wire)
	case *compile.DoubleSpec:
		return fmt.Sprintf("%s.TDouble", wire)
	case *compile.StringSpec, *compile.BinarySpec:
		return fmt.Sprintf("%s.TBinary", wire)
	case *compile.MapSpec:
		return fmt.Sprintf("%s.TMap", wire)
	case *compile.ListSpec:
		return fmt.Sprintf("%s.TList", wire)
	case *compile.SetSpec:
		return fmt.Sprintf("%s.TSet", wire)
	case *compile.EnumSpec:
		return fmt.Sprintf("%s.TI32", wire)
	case *compile.StructSpec:
		return fmt.Sprintf("%s.TStruct", wire)
	default:
		panic(fmt.Sprintf("unknown type spec %v (type %T)", spec, spec))
	}
}
Пример #2
0
// ConstantValuePtr generates an expression which is a pointer to a value of
// type $t.
func ConstantValuePtr(g Generator, c compile.ConstantValue, t compile.TypeSpec) (string, error) {
	var ptrFunc string

	switch compile.RootTypeSpec(t).(type) {
	case *compile.BoolSpec:
		ptrFunc = fmt.Sprintf("%v.Bool", g.Import("go.uber.org/thriftrw/ptr"))
	case *compile.I8Spec:
		ptrFunc = fmt.Sprintf("%v.Int8", g.Import("go.uber.org/thriftrw/ptr"))
	case *compile.I16Spec:
		ptrFunc = fmt.Sprintf("%v.Int16", g.Import("go.uber.org/thriftrw/ptr"))
	case *compile.I32Spec:
		ptrFunc = fmt.Sprintf("%v.Int32", g.Import("go.uber.org/thriftrw/ptr"))
	case *compile.I64Spec:
		ptrFunc = fmt.Sprintf("%v.Int64", g.Import("go.uber.org/thriftrw/ptr"))
	case *compile.DoubleSpec:
		ptrFunc = fmt.Sprintf("%v.Float64", g.Import("go.uber.org/thriftrw/ptr"))
	case *compile.StringSpec:
		ptrFunc = fmt.Sprintf("%v.String", g.Import("go.uber.org/thriftrw/ptr"))
	case *compile.EnumSpec:
		ptrFunc = fmt.Sprintf("_%v_ptr", t.ThriftName())
		err := g.EnsureDeclared(
			`func _<.ThriftName>_ptr(v <typeReference .>) *<typeReference .> {
				return &v
			}`, t)
		if err != nil {
			return "", err
		}
	default:
		return ConstantValue(g, c, t) // not a primitive
	}

	s, err := ConstantValue(g, c, t)
	s = fmt.Sprintf("%v(%v)", ptrFunc, s)
	return s, err
}
Пример #3
0
func constantMap(g Generator, v compile.ConstantMap, t compile.TypeSpec) (string, error) {
	mapSpec := compile.RootTypeSpec(t).(*compile.MapSpec)
	keySpec := mapSpec.KeySpec
	valueSpec := mapSpec.ValueSpec
	return g.TextTemplate(
		`
		<$keyType := .KeySpec>
		<$valueType := .ValueSpec>
		<typeReference .Spec>{
			<range .Value>
				<if isHashable $keyType>
					<constantValue .Key $keyType>:
						<constantValue .Value $valueType>,
				<else>
					{
						Key: <constantValue .Key $keyType>,
						Value: <constantValue .Value $valueType>,
					},
				<end>
			<end>
		}`, struct {
			Spec      compile.TypeSpec
			KeySpec   compile.TypeSpec
			ValueSpec compile.TypeSpec
			Value     compile.ConstantMap
		}{Spec: t, KeySpec: keySpec, ValueSpec: valueSpec, Value: v},
		TemplateFunc("constantValue", ConstantValue))
}
Пример #4
0
func constantList(g Generator, v compile.ConstantList, t compile.TypeSpec) (string, error) {
	valueSpec := compile.RootTypeSpec(t).(*compile.ListSpec).ValueSpec
	return g.TextTemplate(
		`
		<$valueType := .ValueSpec>
		<typeReference .Spec>{
			<range .Value>
				<constantValue . $valueType>,
			<end>
		}`, struct {
			Spec      compile.TypeSpec
			ValueSpec compile.TypeSpec
			Value     compile.ConstantList
		}{Spec: t, ValueSpec: valueSpec, Value: v},
		TemplateFunc("constantValue", ConstantValue))
}
Пример #5
0
func constantStruct(g Generator, v *compile.ConstantStruct, t compile.TypeSpec) (string, error) {
	fields := compile.RootTypeSpec(t).(*compile.StructSpec).Fields
	return g.TextTemplate(
		`
		<$fields := .Fields>
		&<typeName .Spec>{
			<range $name, $value := .Value.Fields>
				<$field := $fields.FindByName $name>
				<if and (not $field.Required) (isPrimitiveType $field.Type)>
					<goName $field>: <constantValuePtr $value $field.Type>,
				<else>
					<goName $field>: <constantValue $value $field.Type>,
				<end>
			<end>
		}`, struct {
			Spec   compile.TypeSpec
			Fields compile.FieldGroup
			Value  *compile.ConstantStruct
		}{Spec: t, Fields: fields, Value: v},
		TemplateFunc("constantValue", ConstantValue),
		TemplateFunc("constantValuePtr", ConstantValuePtr),
	)
}