示例#1
0
func vdlToMojomTypeInternal(t *vdl.Type, outermostType bool, nullable bool, mp map[string]mojom_types.UserDefinedType) (mojomtype mojom_types.Type) {
	switch t.Kind() {
	case vdl.Bool, vdl.Float64, vdl.Float32, vdl.Int8, vdl.Int16, vdl.Int32, vdl.Int64, vdl.Byte, vdl.Uint16, vdl.Uint32, vdl.Uint64:
		return &mojom_types.TypeSimpleType{
			simpleTypeCode(t.Kind()),
		}
	case vdl.String:
		return &mojom_types.TypeStringType{
			stringType(nullable),
		}
	case vdl.Array:
		elem := vdlToMojomTypeInternal(t.Elem(), false, false, mp)
		return &mojom_types.TypeArrayType{
			arrayType(elem, nullable, t.Len()),
		}
	case vdl.List:
		elem := vdlToMojomTypeInternal(t.Elem(), false, false, mp)
		return &mojom_types.TypeArrayType{
			listType(elem, nullable),
		}
	case vdl.Map:
		key := vdlToMojomTypeInternal(t.Key(), false, false, mp)
		elem := vdlToMojomTypeInternal(t.Elem(), false, false, mp)
		return &mojom_types.TypeMapType{
			mapType(key, elem, nullable),
		}
	case vdl.Struct, vdl.Union, vdl.Enum:
		udtKey := addUserDefinedType(t, mp)
		ret := &mojom_types.TypeTypeReference{
			mojom_types.TypeReference{
				Nullable: nullable,
				TypeKey:  &udtKey,
			},
		}
		if !outermostType {
			// This is needed to match the output of the generator exactly, the outermost type
			// is not given an identifier.
			ret.Value.Identifier = ret.Value.TypeKey
		}
		return ret
	case vdl.Optional:
		return vdlToMojomTypeInternal(t.Elem(), false, true, mp)
	default:
		panic(fmt.Sprintf("conversion from VDL kind %v to mojom type not implemented", t.Kind()))
	}
}
示例#2
0
func (t target) fromZero(tt *vdl.Type) error {
	if tt.IsBytes() {
		return t.FromBytes(nil, tt)
	}
	switch tt.Kind() {
	case vdl.Optional:
		elemType := tt.Elem()
		switch elemType.Kind() {
		case vdl.Union, vdl.Struct: // Array? String? Bytes? List? Set?
			// Note: for union, this zeros 16 bytes, but for others it does just 8.
			zeroBytes(t.current.Bytes())
		default:
			panic(fmt.Sprintf("Vdl type %v cannot be optional", tt))
		}
	case vdl.Any:
		panic("Any rep not yet determined")
	case vdl.Bool:
		return t.FromBool(false, tt)
	case vdl.Byte, vdl.Uint16, vdl.Uint32, vdl.Uint64:
		return t.FromUint(0, tt)
	case vdl.Int8, vdl.Int16, vdl.Int32, vdl.Int64:
		return t.FromInt(0, tt)
	case vdl.Float32, vdl.Float64:
		return t.FromFloat(0, tt)
	case vdl.String:
		return t.FromString("", tt)
	case vdl.Enum:
		return t.FromEnumLabel(tt.EnumLabel(0), tt)
	case vdl.TypeObject:
		return t.FromTypeObject(vdl.AnyType)
	case vdl.List:
		lt, err := t.StartList(tt, 0)
		if err != nil {
			return err
		}
		return t.FinishList(lt)
	case vdl.Array:
		lt, err := t.StartList(tt, tt.Len())
		if err != nil {
			return err
		}
		for i := 0; i < tt.Len(); i++ {
			targ, err := lt.StartElem(i)
			if err != nil {
				return err
			}
			if err := targ.(target).fromZero(tt.Elem()); err != nil {
				return err
			}
			if err := lt.FinishElem(targ); err != nil {
				return err
			}
		}
		return t.FinishList(lt)
	case vdl.Map:
		mt, err := t.StartMap(tt, 0)
		if err != nil {
			return err
		}
		return t.FinishMap(mt)
	case vdl.Set:
		st, err := t.StartSet(tt, 0)
		if err != nil {
			return err
		}
		return t.FinishSet(st)
	case vdl.Struct:
		st, err := t.StartFields(tt)
		if err != nil {
			return err
		}
		for i := 0; i < tt.NumField(); i++ {
			fld := tt.Field(i)
			kt, ft, err := st.StartField(fld.Name)
			if err != nil {
				return err
			}
			if err := ft.(target).fromZero(fld.Type); err != nil {
				return err
			}
			if err := st.FinishField(kt, ft); err != nil {
				return err
			}
		}
		return t.FinishFields(st)
	case vdl.Union:
		st, err := t.StartFields(tt)
		if err != nil {
			return err
		}
		fld := tt.Field(0)
		kt, ft, err := st.StartField(fld.Name)
		if err != nil {
			return err
		}
		if err := ft.(target).fromZero(fld.Type); err != nil {
			return err
		}
		if err := st.FinishField(kt, ft); err != nil {
			return err
		}
		return t.FinishFields(st)
	default:
		panic(fmt.Sprintf("unknown type %v", tt))
	}
	return nil
}