Exemplo n.º 1
0
func (self *DataHeader) makeRowDescriptor(fileD *model.FileDescriptor, rootField []*model.FieldDescriptor) bool {

	rowType := model.NewDescriptor()
	rowType.Usage = model.DescriptorUsage_RowType
	rowType.Name = fmt.Sprintf("%sDefine", fileD.Pragma.TableName)
	rowType.Kind = model.DescriptorKind_Struct

	// 类型已经存在, 说明是自己定义的 XXDefine, 不允许
	if _, ok := fileD.DescriptorByName[rowType.Name]; ok {
		log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_UseReservedTypeName), rowType.Name)
		return false
	}

	fileD.Add(rowType)

	// 将表格中的列添加到类型中, 方便导出
	for _, field := range rootField {

		rowType.Add(field)
	}

	return true

}
Exemplo n.º 2
0
func (self *typeModelRoot) ParseData(localFD *model.FileDescriptor, globalFD *model.FileDescriptor) bool {

	var td *model.Descriptor

	reservedRowFieldTypeName := localFD.Pragma.TableName + "Define"

	// 每一行
	for _, m := range self.models {

		self.Row = m.row

		var rawTypeName string

		rawTypeName, self.Col = m.getValue("ObjectType")

		if rawTypeName == reservedRowFieldTypeName {
			log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_UseReservedTypeName), rawTypeName)
			return false
		}

		existType, ok := localFD.DescriptorByName[rawTypeName]

		if ok {

			td = existType

		} else {

			td = model.NewDescriptor()
			td.Name = rawTypeName
			localFD.Add(td)
		}

		// 字段名
		m.fd.Name, self.Col = m.getValue("FieldName")

		// 解析类型
		m.rawFieldType, self.Col = m.getValue("FieldType")
		self.fieldTypeCol = self.Col

		fieldType, complexType, ok := findFieldType(localFD, globalFD, m.rawFieldType)
		if !ok {
			return false
		}

		if fieldType == model.FieldType_None {
			self.unknownModel = append(self.unknownModel, m)
		}

		m.fd.Type = fieldType
		m.fd.Complex = complexType

		var rawFieldValue string
		// 解析值
		rawFieldValue, self.Col = m.getValue("Value")

		kind, enumvalue, ok := parseFieldValue(rawFieldValue)
		if !ok {
			return false
		}

		if td.Kind == model.DescriptorKind_None {
			td.Kind = kind
			// 一些字段有填值, 一些没填值
		} else if td.Kind != kind {
			log.Errorf("%s", i18n.String(i18n.TypeSheet_DescriptorKindNotSame))
			return false
		}

		if td.Kind == model.DescriptorKind_Enum {
			if _, ok := td.FieldByNumber[enumvalue]; ok {
				log.Errorf("%s %d", i18n.String(i18n.TypeSheet_DuplicatedEnumValue), enumvalue)
				return false
			}
		}

		m.fd.EnumValue = enumvalue

		m.fd.Comment, self.Col = m.getValue("Comment")

		var rawMeta string
		rawMeta, self.Col = m.getValue("Meta")

		if err := proto.UnmarshalText(rawMeta, &m.fd.Meta); err != nil {
			log.Errorf("%s, '%s'", i18n.String(i18n.TypeSheet_FieldMetaParseFailed), err.Error())
			return false
		}

		td.Add(&m.fd)

	}

	return true
}