当前位置: 首页>>代码示例>>Golang>>正文


Golang Type.NumField方法代码示例

本文整理汇总了Golang中v/io/v23/vdl.Type.NumField方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.NumField方法的具体用法?Golang Type.NumField怎么用?Golang Type.NumField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在v/io/v23/vdl.Type的用法示例。


在下文中一共展示了Type.NumField方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: neededStructAllocationSize

func neededStructAllocationSize(vt *vdl.Type) uint32 {
	var totalBits uint32
	for fi := 0; fi < vt.NumField(); fi++ {
		field := vt.Field(fi)
		totalBits += baseTypeSizeBits(field.Type)
	}
	return roundBitsTo64Alignment(totalBits)
}
开发者ID:vanadium,项目名称:mojo.v23proxy,代码行数:8,代码来源:allocation_size.go

示例2: unionType

func unionType(t *vdl.Type, mp map[string]mojom_types.UserDefinedType) mojom_types.UserDefinedType {
	unionFields := make([]mojom_types.UnionField, t.NumField())
	for i := 0; i < t.NumField(); i++ {
		unionFields[i] = mojom_types.UnionField{
			DeclData: &mojom_types.DeclarationData{ShortName: strPtr(t.Field(i).Name)},
			Type:     vdlToMojomTypeInternal(t.Field(i).Type, false, false, mp),
			Tag:      uint32(i),
		}
	}
	_, name := vdl.SplitIdent(t.Name())
	return &mojom_types.UserDefinedTypeUnionType{
		mojom_types.MojomUnion{
			DeclData: &mojom_types.DeclarationData{
				ShortName:      strPtr(name),
				FullIdentifier: strPtr(mojomIdentifier(t)),
			},
			Fields: unionFields,
		},
	}
}
开发者ID:vanadium,项目名称:mojo.v23proxy,代码行数:20,代码来源:type.go

示例3: structType

func structType(t *vdl.Type, mp map[string]mojom_types.UserDefinedType) mojom_types.UserDefinedType {
	structFields := make([]mojom_types.StructField, t.NumField())
	for i := 0; i < t.NumField(); i++ {
		structFields[i] = mojom_types.StructField{
			DeclData: &mojom_types.DeclarationData{ShortName: strPtr(t.Field(i).Name)},
			Type:     vdlToMojomTypeInternal(t.Field(i).Type, false, false, mp),
			Offset:   0, // Despite the fact that we can calculated the offset, set it to zero to match the generator
		}
	}
	_, name := vdl.SplitIdent(t.Name())
	return &mojom_types.UserDefinedTypeStructType{
		mojom_types.MojomStruct{
			DeclData: &mojom_types.DeclarationData{
				ShortName:      strPtr(name),
				FullIdentifier: strPtr(mojomIdentifier(t)),
			},
			Fields: structFields,
		},
	}
}
开发者ID:vanadium,项目名称:mojo.v23proxy,代码行数:20,代码来源:type.go

示例4: computeStructLayout

// computeStructLayout computes a representation of the fields in a struct, as
// a list ordered by mojom byte field order.
func computeStructLayout(t *vdl.Type) (layout structLayout) {
	a := structBitAllocation{}

	for i := 0; i < t.NumField(); i++ {
		bits := baseTypeSizeBits(t.Field(i).Type)
		a = allocateStructBits(a, i+1, int(bits))
	}

	lastVal := 0
	for i, v := range a {
		if v != lastVal && v != 0 {
			layout = append(layout, structLayoutField{
				vdlStructIndex: v - 1,
				byteOffset:     uint32(i / 8),
				bitOffset:      uint8(i % 8),
			})
			lastVal = v
		}
	}

	return
}
开发者ID:vanadium,项目名称:mojo.v23proxy,代码行数:24,代码来源:struct_layout.go

示例5: JoinRawBytesAsStruct

func JoinRawBytesAsStruct(targ vdl.Target, structType *vdl.Type, fields []*vom.RawBytes) error {
	st, err := targ.StartFields(structType)
	if err != nil {
		return err
	}
	if structType.NumField() != len(fields) {
		return fmt.Errorf("received %d fields, but %v has %d fields", len(fields), structType, structType.NumField())
	}
	for i := 0; i < structType.NumField(); i++ {
		f := structType.Field(i)
		k, t, err := st.StartField(f.Name)
		if err != nil {
			return err
		}
		if err := fields[i].FillVDLTarget(t, f.Type); err != nil {
			return err
		}
		if err := st.FinishField(k, t); err != nil {
			return err
		}
	}
	return targ.FinishFields(st)
}
开发者ID:vanadium,项目名称:mojo.v23proxy,代码行数:23,代码来源:util.go

示例6: StartFields

func (targ *structSplitTarget) StartFields(tt *vdl.Type) (vdl.FieldsTarget, error) {
	targ.tt = tt
	targ.fields = make([]*vom.RawBytes, tt.NumField())
	return &structSplitFieldsTarget{targ}, nil
}
开发者ID:vanadium,项目名称:mojo.v23proxy,代码行数:5,代码来源:util.go

示例7: 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
//.........这里部分代码省略.........
开发者ID:vanadium,项目名称:mojo.v23proxy,代码行数:101,代码来源:target.go

示例8: transcodeValue


//.........这里部分代码省略.........
		// TODO(toddw): See the comment in encoder.mojomStructSize; we rely on the
		// fields to be presented in the canonical mojom field ordering.
		if !isTopType {
			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")
			}
		}
		_, err := mtv.modec.StartStruct()
		if err != nil {
			return err
		}
		targetFields, err := target.StartFields(vt)
		if err != nil {
			return err
		}
		for _, alloc := range computeStructLayout(vt) {
			mfield := vt.Field(alloc.vdlStructIndex)
			switch vkey, vfield, err := targetFields.StartField(mfield.Name); {
			// TODO(toddw): Handle err == vdl.ErrFieldNoExist case?
			case err != nil:
				return err
			default:
				if err := mtv.transcodeValue(mfield.Type, vfield, false, false); err != nil {
					return err
				}
				if err := targetFields.FinishField(vkey, vfield); err != nil {
					return err
				}
			}
		}
		// TODO(toddw): Fill in fields that weren't decoded with their zero value.
		if err := target.FinishFields(targetFields); err != nil {
			return err
		}
		return mtv.modec.Finish()
	case vdl.Union:
		size, tag, err := mtv.modec.ReadUnionHeader()
		if err != nil {
			return err
		}
		if size == 0 {
			mtv.modec.SkipUnionValue()
			return target.FromNil(vdl.OptionalType(vt))
		}
		if int(tag) >= vt.NumField() {
			return fmt.Errorf("union tag out of bounds")
		}
		fld := vt.Field(int(tag))
		targetFields, err := target.StartFields(vt)
		if err != nil {
			return err
		}
		vKey, vField, err := targetFields.StartField(fld.Name)
		if err != nil {
			return err
		}
		if fld.Type.Kind() == vdl.Union {
			switch ptr, err := mtv.modec.ReadPointer(); {
			case err != nil:
				return err
			case ptr == 0 && isNullable:
				return target.FromNil(vdl.OptionalType(fld.Type))
			case ptr == 0 && !isNullable:
				return fmt.Errorf("invalid null union pointer")
			}
			if err := mtv.modec.StartNestedUnion(); err != nil {
				return err
			}
		}
		if err := mtv.transcodeValue(fld.Type, vField, false, false); err != nil {
			return err
		}
		if fld.Type.Kind() == vdl.Union {
			if err := mtv.modec.Finish(); err != nil {
				return err
			}
		}
		if err := targetFields.FinishField(vKey, vField); err != nil {
			return err
		}
		if err := target.FinishFields(targetFields); err != nil {
			return err
		}
		mtv.modec.FinishReadingUnionValue()
		return nil
	case vdl.Optional:
		return mtv.transcodeValue(vt.Elem(), target, false, true)
	case vdl.Any:
		panic("unimplemented")
	//case vdl.TypeObject:
	//	panic("unimplemented")
	default:
		panic(fmt.Errorf("decodeValue unhandled vdl type: %v", vt))
	}
}
开发者ID:vanadium,项目名称:mojo.v23proxy,代码行数:101,代码来源:from_mojom.go


注:本文中的v/io/v23/vdl.Type.NumField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。