Example #1
0
func (self protoFieldDescriptor) TypeString() string {
	if self.Complex != nil {
		return self.Complex.Name
	} else {
		return model.FieldTypeToString(self.Type)
	}
}
Example #2
0
func (self *BinaryFile) WriteNodeValue(ft model.FieldType, value *model.Node) {

	switch ft {
	case model.FieldType_Int32:
		v, _ := strconv.ParseInt(value.Value, 10, 32)
		binary.Write(&self.buf, binary.LittleEndian, int32(v))
	case model.FieldType_UInt32:
		v, _ := strconv.ParseUint(value.Value, 10, 32)

		binary.Write(&self.buf, binary.LittleEndian, uint32(v))
	case model.FieldType_Int64:
		v, _ := strconv.ParseInt(value.Value, 10, 64)

		binary.Write(&self.buf, binary.LittleEndian, int64(v))
	case model.FieldType_UInt64:
		v, _ := strconv.ParseUint(value.Value, 10, 64)

		binary.Write(&self.buf, binary.LittleEndian, uint64(v))
	case model.FieldType_Float:
		v, _ := strconv.ParseFloat(value.Value, 32)

		binary.Write(&self.buf, binary.LittleEndian, float32(v))
	case model.FieldType_Bool:
		v, _ := strconv.ParseBool(value.Value)

		binary.Write(&self.buf, binary.LittleEndian, v)
	case model.FieldType_String:
		self.WriteString(value.Value)
	case model.FieldType_Enum:
		binary.Write(&self.buf, binary.LittleEndian, value.EnumValue)
	default:
		panic("unsupport type" + model.FieldTypeToString(ft))
	}

}
Example #3
0
func (self *goIndexModel) KeyType() string {

	if self.Type == model.FieldType_Enum {
		return fmt.Sprintf("%s.%s", self.FieldDescriptor.Parent.File.Pragma.Package, self.Complex.Name)
	}

	return model.FieldTypeToString(self.Type)
}
Example #4
0
// 检查字段行的长度
func (self *DataHeader) ParseProtoField(index int, sheet *Sheet, localFD *model.FileDescriptor, globalFD *model.FileDescriptor) bool {

	var def *model.FieldDescriptor

	// 遍历列
	for sheet.Column = 0; ; sheet.Column++ {

		def = new(model.FieldDescriptor)

		// ====================解析字段====================
		def.Name = sheet.GetCellData(DataSheetRow_FieldName, sheet.Column)
		if def.Name == "" {
			break
		}

		// #开头表示注释, 跳过
		if strings.Index(def.Name, "#") != 0 {

			// ====================解析类型====================

			testFileD := localFD

			rawFieldType := sheet.GetCellData(DataSheetRow_FieldType, sheet.Column)

			for {
				if def.ParseType(testFileD, rawFieldType) {
					break
				}

				if testFileD == localFD {
					testFileD = globalFD
					continue
				}

				break
			}

			// 依然找不到, 报错
			if def.Type == model.FieldType_None {
				sheet.Row = DataSheetRow_FieldType
				log.Errorf("%s, '%s' (%s) raw: %s", i18n.String(i18n.DataHeader_TypeNotFound), def.Name, model.FieldTypeToString(def.Type), rawFieldType)
				goto ErrorStop
			}

			// ====================解析特性====================
			metaString := sheet.GetCellData(DataSheetRow_FieldMeta, sheet.Column)

			if err := proto.UnmarshalText(metaString, &def.Meta); err != nil {
				sheet.Row = DataSheetRow_FieldMeta
				log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_MetaParseFailed), err)
				goto ErrorStop
			}

			def.Comment = sheet.GetCellData(DataSheetRow_Comment, sheet.Column)

			// 根据字段名查找, 处理repeated字段case
			exist, ok := self.HeaderByName[def.Name]

			if ok {

				// 多个同名字段只允许repeated方式的字段
				if !exist.IsRepeated {
					sheet.Row = DataSheetRow_FieldName
					log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_DuplicateFieldName), def.Name)
					goto ErrorStop
				}

				// 多个repeated描述类型不一致
				if exist.Type != def.Type {
					sheet.Row = DataSheetRow_FieldType

					log.Errorf("%s '%s' '%s' '%s'", i18n.String(i18n.DataHeader_RepeatedFieldTypeNotSameInMultiColumn),
						def.Name,
						model.FieldTypeToString(exist.Type),
						model.FieldTypeToString(def.Type))

					goto ErrorStop
				}

				// 多个repeated描述内建类型不一致
				if exist.Complex != def.Complex {
					sheet.Row = DataSheetRow_FieldType

					log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_RepeatedFieldTypeNotSameInMultiColumn),
						def.Name)

					goto ErrorStop
				}

				// 多个repeated描述的meta不一致
				if proto.CompactTextString(&exist.Meta) != proto.CompactTextString(&def.Meta) {
					sheet.Row = DataSheetRow_FieldMeta

					log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_RepeatedFieldMetaNotSameInMultiColumn),
						def.Name)

					goto ErrorStop
				}

				def = exist

			} else {
				self.HeaderByName[def.Name] = def
				self.headerFields = append(self.headerFields, def)
			}
		}

		// 有注释字段, 但是依然要放到这里来进行索引
		self.rawHeaderFields = append(self.rawHeaderFields, def)
	}

	if len(self.rawHeaderFields) == 0 {
		return false
	}

	if index == 0 {
		// 添加第一个数据表的定义
		if !self.makeRowDescriptor(localFD, self.headerFields) {
			goto ErrorStop
		}
	}

	return true

ErrorStop:

	r, c := sheet.GetRC()

	log.Errorf("%s|%s(%s)", sheet.file.FileName, sheet.Name, util.ConvR1C1toA1(r, c))
	return false
}