コード例 #1
0
ファイル: type.go プロジェクト: vanadium/mojo.v23proxy
// Given a mojom Type and the descriptor mapping, produce the corresponding vdltype.
func mojomToVDLType(mojomtype mojom_types.Type, mp map[string]mojom_types.UserDefinedType, builder *vdl.TypeBuilder, pendingUdts map[string]vdl.TypeOrPending) (vt vdl.TypeOrPending) {
	mt := interface{}(mojomtype)
	switch mt := interface{}(mt).(type) { // To do the type switch, mt has to be converted to interface{}.
	case *mojom_types.TypeSimpleType: // TypeSimpleType
		switch mt.Value {
		case mojom_types.SimpleType_Bool:
			vt = vdl.BoolType
		case mojom_types.SimpleType_Double:
			vt = vdl.Float64Type
		case mojom_types.SimpleType_Float:
			vt = vdl.Float32Type
		case mojom_types.SimpleType_Int8:
			vt = vdl.Int8Type
		case mojom_types.SimpleType_Int16:
			vt = vdl.Int16Type
		case mojom_types.SimpleType_Int32:
			vt = vdl.Int32Type
		case mojom_types.SimpleType_Int64:
			vt = vdl.Int64Type
		case mojom_types.SimpleType_Uint8:
			vt = vdl.ByteType
		case mojom_types.SimpleType_Uint16:
			vt = vdl.Uint16Type
		case mojom_types.SimpleType_Uint32:
			vt = vdl.Uint32Type
		case mojom_types.SimpleType_Uint64:
			vt = vdl.Uint64Type
		}
	case *mojom_types.TypeStringType: // TypeStringType
		st := mt.Value
		if st.Nullable {
			panic("nullable strings don't exist in vdl")
		}
		vt = vdl.StringType
	case *mojom_types.TypeArrayType: // TypeArrayType
		at := mt.Value
		if at.Nullable {
			panic("nullable arrays don't exist in vdl")
		}
		if at.FixedLength > 0 {
			vt = builder.Array().
				AssignLen(int(at.FixedLength)).
				AssignElem(mojomToVDLType(at.ElementType, mp, builder, pendingUdts))
		} else {
			vt = builder.List().
				AssignElem(mojomToVDLType(at.ElementType, mp, builder, pendingUdts))
		}
	case *mojom_types.TypeMapType: // TypeMapType
		// Note that mojom doesn't have sets.
		m := mt.Value
		if m.Nullable {
			panic("nullable maps don't exist in vdl")
		}
		vt = builder.Map().
			AssignKey(mojomToVDLType(m.KeyType, mp, builder, pendingUdts)).
			AssignElem(mojomToVDLType(m.ValueType, mp, builder, pendingUdts))
	case *mojom_types.TypeHandleType: // TypeHandleType
		panic("handles don't exist in vdl")
	case *mojom_types.TypeTypeReference: // TypeTypeReference
		tr := mt.Value
		if tr.IsInterfaceRequest {
			panic("interface requests don't exist in vdl")
		}
		udt := mp[*tr.TypeKey]
		var ok bool
		vt, ok = pendingUdts[*tr.TypeKey]
		if !ok {
			vt = mojomToVDLTypeUDT(*tr.TypeKey, udt, mp, builder, pendingUdts)
		}
		if tr.Nullable {
			if udt.Tag() != 1 {
				panic("nullable non-struct type reference cannot be represented in vdl")
			}
			vt = builder.Optional().AssignElem(vt)
		}
	default:
		panic(fmt.Errorf("%#v has unknown tag %d", mojomtype, mojomtype.Tag()))
	}

	return vt
}