Exemplo n.º 1
0
func wrapCSharpDefaultValue(fd *model.FieldDescriptor) string {
	switch fd.Type {
	case model.FieldType_Enum:
		return fmt.Sprintf("%s.%s", fd.Complex.Name, fd.DefaultValue())
	case model.FieldType_String:
		return fmt.Sprintf("\"%s\"", fd.DefaultValue())
	}

	return fd.DefaultValue()
}
Exemplo n.º 2
0
func dataProcessor(file *File, fd *model.FieldDescriptor, raw string, node *model.Node) bool {

	// 列表
	if fd.IsRepeated {

		spliter := fd.ListSpliter()

		// 使用多格子实现的repeated
		if spliter == "" {

			if _, ok := filter.ConvertValue(fd, raw, file.GlobalFD, node); !ok {
				goto ConvertError
			}

		} else {
			// 一个格子切割的repeated

			valueList := strings.Split(raw, spliter)

			for _, v := range valueList {

				if _, ok := filter.ConvertValue(fd, v, file.GlobalFD, node); !ok {
					goto ConvertError
				}
			}

		}

	} else {

		// 单值
		if cv, ok := filter.ConvertValue(fd, raw, file.GlobalFD, node); !ok {
			goto ConvertError

		} else {

			// 值重复检查
			if fd.Meta.RepeatCheck && !file.checkValueRepeat(fd, cv) {
				log.Errorf("%s, %s raw: '%s'", i18n.String(i18n.DataSheet_ValueRepeated), fd.String(), cv)
				return false
			}
		}

	}

	return true

ConvertError:

	log.Errorf("%s, %s raw: '%s'", i18n.String(i18n.DataSheet_ValueConvertError), fd.String(), raw)

	return false
}
Exemplo n.º 3
0
// 从单元格原始数据到最终输出的数值, 检查并转换, 处理默认值及根据meta转换情况
func ConvertValue(fd *model.FieldDescriptor, value string, fileD *model.FileDescriptor, node *model.Node) (ret string, ok bool) {

	// 空格, 且有默认值时, 使用默认值
	if value == "" {
		value = fd.DefaultValue()
	}

	switch fd.Type {
	case model.FieldType_Int32:
		_, err := strconv.ParseInt(value, 10, 32)
		if err != nil {
			log.Debugln(err)
			return "", false
		}

		ret = value
		node.AddValue(ret)
	case model.FieldType_Int64:
		_, err := strconv.ParseInt(value, 10, 64)

		if err != nil {
			log.Debugln(err)
			return "", false
		}

		ret = value
		node.AddValue(ret)
	case model.FieldType_UInt32:
		_, err := strconv.ParseUint(value, 10, 32)
		if err != nil {
			log.Debugln(err)
			return "", false
		}

		ret = value
		node.AddValue(ret)
	case model.FieldType_UInt64:
		_, err := strconv.ParseUint(value, 10, 64)
		if err != nil {
			log.Debugln(err)
			return "", false
		}

		ret = value
		node.AddValue(ret)
	case model.FieldType_Float:
		_, err := strconv.ParseFloat(value, 32)
		if err != nil {
			log.Debugln(err)
			return "", false
		}

		ret = value
		node.AddValue(ret)
	case model.FieldType_Bool:

		for {
			if value == "是" {
				ret = "true"
				break
			} else if value == "否" {
				ret = "false"
				break
			}

			v, err := strconv.ParseBool(value)

			if err != nil {
				log.Debugln(err)
				return "", false
			}

			if v {
				ret = "true"
			} else {
				ret = "false"
			}

			break
		}

		node.AddValue(ret)

	case model.FieldType_String:
		ret = value
		node.AddValue(ret)
	case model.FieldType_Enum:
		if fd.Complex == nil {
			log.Errorf("%s, '%s'", i18n.String(i18n.ConvertValue_EnumTypeNil), fd.Name)
			return "", false
		}

		evd := fd.Complex.FieldByValueAndMeta(value)
		if evd == nil {
			log.Errorf("%s, '%s' '%s'", i18n.String(i18n.ConvertValue_EnumValueNotFound), value, fd.Complex.Name)
			return "", false
		}

		// 使用枚举的英文字段名输出
		ret = evd.Name
		node.AddValue(ret).EnumValue = evd.EnumValue

	case model.FieldType_Struct:

		if fd.Complex == nil {
			log.Errorf("%s, '%s'", i18n.String(i18n.ConvertValue_StructTypeNil), fd.Name)
			return "", false
		}

		if !parseStruct(fd, value, fileD, node) {
			return "", false
		}

	default:
		log.Errorf("%s, '%s' '%s'", i18n.String(i18n.ConvertValue_UnknownFieldType), fd.Name, fd.Name)
		return "", false
	}

	ok = true

	return
}
Exemplo n.º 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
}
Exemplo n.º 5
0
// 合并每个表带的类型
func (self *Globals) AddContent(tab *model.Table) bool {

	localFD := tab.LocalFD

	self.guard.Lock()

	defer self.guard.Unlock()

	// 有表格里描述的包名不一致, 无法合成最终的文件
	if self.Pragma.Package == "" {
		self.Pragma.Package = localFD.Pragma.Package
	} else if self.Pragma.Package != localFD.Pragma.Package {

		log.Errorf("%s, '%s' '%s'", i18n.String(i18n.Globals_PackageNameDiff), self.Pragma.Package, localFD.Pragma.Package)
		return false
	}

	if _, ok := self.tableByName[localFD.Name]; ok {

		log.Errorf("%s, '%s'", i18n.String(i18n.Globals_TableNameDuplicated), localFD.Name)
		return false
	}

	// 表的全局类型信息与合并信息一致
	tab.GlobalFD = self.FileDescriptor

	self.tableByName[localFD.Name] = tab
	self.Tables = append(self.Tables, tab)

	// 每个表在结构体里的字段
	var rowFD model.FieldDescriptor
	rowFD.Name = localFD.Name
	rowFD.Type = model.FieldType_Struct
	rowFD.Complex = localFD.RowDescriptor()
	rowFD.IsRepeated = true
	rowFD.Order = int32(len(self.CombineStruct.Fields) + 1)
	rowFD.Comment = localFD.Name
	self.CombineStruct.Add(&rowFD)

	if localFD.RowDescriptor() == nil {
		panic("row field null:" + localFD.Name)
	}

	for _, d := range localFD.Descriptors {

		// 非行类型的, 全部忽略
		if d.Usage != model.DescriptorUsage_RowType {
			continue
		}

		for _, indexFD := range d.Indexes {

			key := TableIndex{
				Row:   &rowFD,
				Index: indexFD,
			}

			self.GlobalIndexes = append(self.GlobalIndexes, key)

		}

	}

	return true
}