Esempio n. 1
0
File: msg.go Progetto: davyxu/tabtoy
// 添加消息数组
func (self *DynamicMessage) AddRepeatedMessage(fd *pbmeta.FieldDescriptor, value *DynamicMessage) bool {

	if fd == nil || !self.Desc.Contains(fd) {
		log.Errorf("field not found: '%s' in '%s'", fd.Name(), self.Desc.Name())
		return false
	}

	if fd.Type() != pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
		log.Errorf("field is not message: '%s' in '%s'", fd.Name(), self.Desc.Name())
		return false
	}

	fv := self.fetchValue(fd, true)

	if !fd.IsRepeated() {

		log.Errorf("field is non-repeated by used like repeated value", fd.Name())

		return false
	}

	if fv.msgArray == nil {
		fv.msgArray = make([]*DynamicMessage, 0)
	}

	fv.msgArray = append(fv.msgArray, value)

	return true
}
Esempio n. 2
0
File: msg.go Progetto: jwk000/tabtoy
// 添加值数组
func (self *DynamicMessage) AddRepeatedValue(fd *pbmeta.FieldDescriptor, value string) bool {

	if fd == nil || !self.Desc.Contains(fd) {
		log.Errorf("field not found: '%s' in '%s', value: '%s'", fd.Name(), self.Desc.Name(), value)
		return false
	}

	if fd.Type() == pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
		log.Errorf("field is message not value: '%s' in '%s', value: '%s'", fd.Name(), self.Desc.Name(), value)
		return false
	}

	fv := self.fetchValue(fd, true)

	if !fd.IsRepeated() {
		return false
	}

	if fv.valueArray == nil {
		fv.valueArray = make([]string, 0)
	}

	fv.valueArray = append(fv.valueArray, value)

	return true
}
Esempio n. 3
0
func fieldTypeString(fd *pbmeta.FieldDescriptor) string {

	var ret string

	switch fd.Type() {

	case pbprotos.FieldDescriptorProto_TYPE_INT32:
		ret = "int"
	case pbprotos.FieldDescriptorProto_TYPE_UINT32:
		ret = "uint"
	case pbprotos.FieldDescriptorProto_TYPE_BOOL:
		ret = "bool"
	case pbprotos.FieldDescriptorProto_TYPE_FLOAT:
		ret = "float"
	case pbprotos.FieldDescriptorProto_TYPE_DOUBLE:
		ret = "double"
	case pbprotos.FieldDescriptorProto_TYPE_STRING:
		ret = "string"
	case pbprotos.FieldDescriptorProto_TYPE_INT64:
		ret = "long"
	case pbprotos.FieldDescriptorProto_TYPE_ENUM,
		pbprotos.FieldDescriptorProto_TYPE_MESSAGE:
		ret = fd.FullTypeName()
	default:
		ret = "unknown"
	}

	if fd.IsRepeated() {
		return fmt.Sprintf("global::System.Collections.Generic.List<%s>", ret)
	}

	return ret
}
Esempio n. 4
0
// 普通值
func (self *jsonWriter) WriteValue(fd *pbmeta.FieldDescriptor, value string, indent int) {

	if fd.IsRepeated() {
		self.printer.WriteString(jsonvalueWrapper(fd, value))
	} else {
		self.printer.WriteString(fmt.Sprintf("\"%s\": %s", fd.Name(), jsonvalueWrapper(fd, value)))
	}

}
Esempio n. 5
0
File: msg.go Progetto: davyxu/tabtoy
// 设置单一值
func (self *DynamicMessage) SetValue(fd *pbmeta.FieldDescriptor, value string) bool {

	if fd == nil || !self.Desc.Contains(fd) {
		log.Errorf("field not found: '%s' in '%s', value: '%s'", fd.Name(), self.Desc.Name(), value)
		return false
	}

	if fd.Type() == pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
		log.Errorf("field is message not value: '%s' in '%s', value: '%s'", fd.Name(), self.Desc.Name(), value)
		return false
	}

	fv := self.fetchValue(fd, false)

	if fd.IsRepeated() {

		log.Errorf("field is repeated by used like non-repeated value", fd.Name())

		return false

	}

	// 可选字段
	if fd.IsOptional() {

		var existValue string
		if fv != nil {
			existValue = fv.value
		}

		// 有指派默认值
		if fd.DefaultValue() != "" {
			existValue = fd.DefaultValue()
		} else {
			// 没有指派默认值, 取值的默认值
			existValue = GetDefaultValue(fd)
		}

		// 输入值和已经存在的值一致, 就无需设置了
		if existValue == value {
			return true
		}

	}

	if fv == nil {
		fv = self.addValue(fd)
	}

	fv.value = value

	return true
}
Esempio n. 6
0
File: lua.go Progetto: davyxu/tabtoy
// Value是消息的字段
func (self *luaWriter) WriteMessage(fd *pbmeta.FieldDescriptor, msg *data.DynamicMessage, indent int) {

	if indent == 1 || fd.IsRepeated() {
		self.printer.WriteString("{")

	} else {
		self.printer.WriteString(fmt.Sprintf("%s = {", fd.Name()))
	}

	rawWriteMessage(&self.printer, self, msg, indent)

	self.printer.WriteString("}")

}
Esempio n. 7
0
File: msg.go Progetto: davyxu/tabtoy
// 取消息数组
func (self *DynamicMessage) GetRepeatedMessage(fd *pbmeta.FieldDescriptor) []*DynamicMessage {

	if fd.Type() != pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
		return nil
	}

	fv, ok := self.fieldMap[fd]

	if !ok {
		return nil
	}

	if !fd.IsRepeated() {
		return nil
	}

	return fv.msgArray
}
Esempio n. 8
0
File: msg.go Progetto: davyxu/tabtoy
// 取单一值
func (self *DynamicMessage) GetValue(fd *pbmeta.FieldDescriptor) (string, bool) {

	if fd.Type() == pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
		return "", false
	}

	fv, ok := self.fieldMap[fd]

	if !ok {
		return "", false
	}

	if fd.IsRepeated() {
		return "", false
	}

	return fv.value, true
}
Esempio n. 9
0
// Value是消息的字段
func (self *jsonWriter) WriteMessage(fd *pbmeta.FieldDescriptor, msg *data.DynamicMessage, indent int) {

	pos := self.printer.Len()

	if indent == 1 || fd.IsRepeated() {
		self.printer.WriteString("{")

	} else {
		self.printer.WriteString(fmt.Sprintf("\"%s\" : {", fd.Name()))
	}

	valueWrite := rawWriteMessage(&self.printer, self, msg, indent)

	self.printer.WriteString("}")

	// 如果没写入值, 将回滚写入
	if valueWrite == 0 {
		self.printer.Truncate(pos)
	}
}
Esempio n. 10
0
File: msg.go Progetto: davyxu/tabtoy
// 单一消息
func (self *DynamicMessage) SetMessage(fd *pbmeta.FieldDescriptor, value *DynamicMessage) bool {

	if fd == nil || !self.Desc.Contains(fd) {
		log.Errorf("field not found: '%s' in '%s'", fd.Name(), self.Desc.Name())
		return false
	}

	if fd.Type() != pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
		log.Errorf("field is not message: '%s' in '%s'", fd.Name(), self.Desc.Name())
		return false
	}

	if fd.IsRepeated() {
		log.Errorf("field is repeated by used like non-repeated value", fd.Name())

		return false
	}

	self.fetchValue(fd, true).msg = value

	return true
}
Esempio n. 11
0
func printField(gen *Generator, fd *pbmeta.FieldDescriptor, msg *pbmeta.Descriptor, file *pbmeta.FileDescriptor) {
	typeStr := fieldTypeString(fd)
	memberVar := "_" + fd.Name()

	//  private int _Age = default(int);

	if fd.IsRepeated() {
		gen.Println("private readonly ", typeStr, " ", memberVar, " = new ", typeStr, "();")

	} else {

		gen.Println("private ", typeStr, " ", memberVar, " = ", getDefaultValue(fd), ";")

	}

	// [global::ProtoBuf.ProtoMember(10, IsRequired = false, Name=@"Age", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
	gen.BeginLine()

	gen.Print("[global::ProtoBuf.ProtoMember(", fd.Define.GetNumber())

	if !fd.IsRepeated() {
		gen.Print(", IsRequired = ", isRequired(fd))
	}

	gen.Print(", Name=@\"", fd.Name(), "\", DataFormat = global::ProtoBuf.DataFormat.", getDataFormat(fd), ")]")

	gen.EndLine()

	if !fd.IsRepeated() {
		gen.Println("[global::System.ComponentModel.DefaultValue(", getDefaultValue(fd), ")]")
	}

	// 属性定义
	gen.Println("public ", typeStr, " ", fd.Name())
	gen.Println("{")
	gen.In()
	gen.Println("get { return ", memberVar, "; }")

	if !fd.IsRepeated() {
		gen.Println("set { ", memberVar, " = value; }")
	}

	gen.Out()
	gen.Println("}")

}