Exemple #1
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
}
Exemple #2
0
func valueName(spec compile.TypeSpec) string {
	switch s := spec.(type) {
	case *compile.MapSpec:
		return fmt.Sprintf(
			"Map_%s_%s", valueName(s.KeySpec), valueName(s.ValueSpec),
		)
	case *compile.ListSpec:
		return fmt.Sprintf("List_%s", valueName(s.ValueSpec))
	case *compile.SetSpec:
		return fmt.Sprintf("Set_%s", valueName(s.ValueSpec))
	default:
		return goCase(spec.ThriftName())
	}
}