本文整理汇总了Golang中v/io/v23/vdl.Type.Kind方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.Kind方法的具体用法?Golang Type.Kind怎么用?Golang Type.Kind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类v/io/v23/vdl.Type
的用法示例。
在下文中一共展示了Type.Kind方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: FromFloat
func (t target) FromFloat(src float64, tt *vdl.Type) error {
switch tt.Kind() {
case vdl.Float32:
binary.LittleEndian.PutUint32(t.current.Bytes(), math.Float32bits(float32(src)))
case vdl.Float64:
binary.LittleEndian.PutUint64(t.current.Bytes(), math.Float64bits(src))
default:
return fmt.Errorf("invalid FromFloat(%v, %v)", src, tt)
}
return nil
}
示例2: StartFields
func (t target) StartFields(tt *vdl.Type) (vdl.FieldsTarget, error) {
if tt.Kind() == vdl.Optional {
tt = tt.Elem()
}
if tt.Kind() == vdl.Union {
return unionFieldsTarget{
vdlType: tt,
block: t.current,
}, nil
}
fieldsTarget, block, err := structFieldShared(tt, t.allocator(), true)
t.writePointer(block)
return fieldsTarget, err
}
示例3: FromInt
func (t target) FromInt(src int64, tt *vdl.Type) error {
switch tt.Kind() {
case vdl.Int8:
t.current.Bytes()[0] = byte(src)
case vdl.Int16:
binary.LittleEndian.PutUint16(t.current.Bytes(), uint16(src))
case vdl.Int32:
binary.LittleEndian.PutUint32(t.current.Bytes(), uint32(src))
case vdl.Int64:
binary.LittleEndian.PutUint64(t.current.Bytes(), uint64(src))
default:
return fmt.Errorf("invalid FromInt(%v, %v)", src, tt)
}
return nil
}
示例4: baseTypeSizeBits
func baseTypeSizeBits(vt *vdl.Type) uint32 {
switch vt.Kind() {
case vdl.Bool:
return 1
case vdl.Byte, vdl.Int8:
return 8
case vdl.Uint16, vdl.Int16:
return 16
case vdl.Uint32, vdl.Int32, vdl.Float32, vdl.Enum:
return 32
case vdl.Union:
return 128 // Header + value / pointer to inner union
default: // Either Uint64, Int64, Float64 or pointer.
return 64
}
}
示例5: StartSet
// TODO(bprosnitz) This uses list, should we use map instead?
func (t target) StartSet(tt *vdl.Type, len int) (vdl.SetTarget, error) {
if tt.Kind() == vdl.Optional {
tt = tt.Elem()
}
bitsNeeded := baseTypeSizeBits(tt.Key()) * uint32(len)
block := t.allocator().Allocate((bitsNeeded+7)/8, uint32(len))
t.writePointer(block)
if tt.Key().Kind() == vdl.Bool {
return &bitListTarget{
block: block,
}, nil
} else {
return &listTarget{
incrementSize: baseTypeSizeBits(tt.Key()) / 8,
block: block,
}, nil
}
}
示例6: addUserDefinedType
func addUserDefinedType(t *vdl.Type, mp map[string]mojom_types.UserDefinedType) string {
key := mojomTypeKey(t)
if _, ok := mp[key]; ok {
return key
}
mp[key] = nil // placeholder to stop recursion
var udt mojom_types.UserDefinedType
switch t.Kind() {
case vdl.Struct:
udt = structType(t, mp)
case vdl.Union:
udt = unionType(t, mp)
case vdl.Enum:
udt = enumType(t)
default:
panic(fmt.Sprintf("conversion from VDL kind %v to mojom user defined type not implemented", t.Kind()))
}
mp[key] = udt
return key
}
示例7: StartMap
func (t target) StartMap(tt *vdl.Type, len int) (vdl.MapTarget, error) {
if tt.Kind() == vdl.Optional {
tt = tt.Elem()
}
pointerBlock := t.allocator().Allocate(16, 0)
t.writePointer(pointerBlock)
st := target{
current: pointerBlock.Slice(0, 8),
}
keys, err := st.StartSet(vdl.SetType(tt.Key()), len)
if err != nil {
return nil, err
}
valuePlaceholder := target{
current: pointerBlock.Slice(8, 16),
}
return &mapTarget{
keys: keys,
valuePlaceholder: valuePlaceholder,
valueType: tt.Elem(),
}, nil
}
示例8: vdlToMojomTypeInternal
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()))
}
}
示例9: FromNil
func (t target) FromNil(tt *vdl.Type) error {
if tt.Kind() == vdl.Optional || tt.Kind() == vdl.Any {
return t.fromZero(tt)
}
return fmt.Errorf("FromNil called on non-nillable type")
}
示例10: fromZero
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
//.........这里部分代码省略.........
示例11: transcodeValue
func (mtv *mojomToTargetTranscoder) transcodeValue(vt *vdl.Type, target vdl.Target, isTopType, isNullable bool) error {
switch vt.Kind() {
case vdl.Bool:
value, err := mtv.modec.ReadBool()
if err != nil {
return err
}
return target.FromBool(value, vt)
case vdl.Int8:
value, err := mtv.modec.ReadInt8()
if err != nil {
return err
}
return target.FromInt(int64(value), vt)
case vdl.Int16:
value, err := mtv.modec.ReadInt16()
if err != nil {
return err
}
return target.FromInt(int64(value), vt)
case vdl.Int32:
value, err := mtv.modec.ReadInt32()
if err != nil {
return err
}
return target.FromInt(int64(value), vt)
case vdl.Int64:
value, err := mtv.modec.ReadInt64()
if err != nil {
return err
}
return target.FromInt(value, vt)
case vdl.Byte:
value, err := mtv.modec.ReadUint8()
if err != nil {
return err
}
return target.FromUint(uint64(value), vt)
case vdl.Uint16:
value, err := mtv.modec.ReadUint16()
if err != nil {
return err
}
return target.FromUint(uint64(value), vt)
case vdl.Uint32:
value, err := mtv.modec.ReadUint32()
if err != nil {
return err
}
return target.FromUint(uint64(value), vt)
case vdl.Uint64:
value, err := mtv.modec.ReadUint64()
if err != nil {
return err
}
return target.FromUint(value, vt)
case vdl.Float32:
value, err := mtv.modec.ReadFloat32()
if err != nil {
return err
}
return target.FromFloat(float64(value), vt)
case vdl.Float64:
value, err := mtv.modec.ReadFloat64()
if err != nil {
return err
}
return target.FromFloat(value, vt)
case vdl.String:
switch ptr, err := mtv.modec.ReadPointer(); {
case err != nil:
return err
case ptr == 0:
return fmt.Errorf("invalid null string pointer")
default:
value, err := mtv.modec.ReadString()
if err != nil {
return err
}
return target.FromString(value, vt)
}
return nil
case vdl.Enum:
index, err := mtv.modec.ReadInt32()
if err != nil {
return err
}
if int(index) >= vt.NumEnumLabel() || index < 0 {
return fmt.Errorf("enum label out of range")
}
target.FromEnumLabel(vt.EnumLabel(int(index)), vt)
return nil
case vdl.Array, vdl.List:
switch ptr, err := mtv.modec.ReadPointer(); {
case err != nil:
return err
case ptr == 0 && isNullable:
return target.FromNil(vdl.OptionalType(vt))
case ptr == 0 && !isNullable:
return fmt.Errorf("invalid null struct pointer")
//.........这里部分代码省略.........