示例#1
0
文件: merge.go 项目: gogo/fieldpath
func getFieldNumber(descriptorSet *descriptor.FileDescriptorSet, rootPkg string, rootMsg string, msg *descriptor.DescriptorProto, fieldNum int32) *descriptor.FieldDescriptorProto {
	for _, f := range msg.GetField() {
		if f.GetNumber() == fieldNum {
			return f
		}
	}
	if !msg.IsExtendable() {
		return nil
	}
	extendee := "." + rootPkg + "." + rootMsg
	for _, file := range descriptorSet.GetFile() {
		for _, ext := range file.GetExtension() {
			if strings.Map(dotToUnderscore, file.GetPackage()) == strings.Map(dotToUnderscore, rootPkg) {
				if !(ext.GetExtendee() == rootMsg || ext.GetExtendee() == extendee) {
					continue
				}
			} else {
				if ext.GetExtendee() != extendee {
					continue
				}
			}
			if ext.GetNumber() == fieldNum {
				return ext
			}
		}
	}
	return nil
}
示例#2
0
文件: merge.go 项目: gogo/fieldpath
// The NoMerge function checks that the marshaled protocol buffer does not require any merging when unmarshaling.
// When this property holds, streaming processing is possible.
//
// See below quotes from the protocol buffer documentation that describes how merging should work.
//
// https://developers.google.com/protocol-buffers/docs/encoding#optional
//
// Normally, an encoded message would never have more than one instance of an optional or required field.
// However, parsers are expected to handle the case in which they do.
// For numeric types and strings, if the same value appears multiple times, the parser accepts the last value it sees.
// For embedded message fields, the parser merges multiple instances of the same field,
// as if with the Message::MergeFrom method – that is,
// all singular scalar fields in the latter instance replace those in the former,
// singular embedded messages are merged, and repeated fields are concatenated.
// The effect of these rules is that parsing the concatenation of two encoded messages produces
// exactly the same result as if you had parsed the two messages separately and merged the resulting objects.
// That is, this:
//
// MyMessage message;
// message.ParseFromString(str1 + str2);
//
// is equivalent to this:
//
// MyMessage message, message2;
// message.ParseFromString(str1);
// message2.ParseFromString(str2);
// message.MergeFrom(message2);
//
// This property is occasionally useful, as it allows you to merge two messages even if you do not know their types.
//
// https://developers.google.com/protocol-buffers/docs/encoding#order
//
// However, protocol buffer parsers must be able to parse fields in any order,
// as not all messages are created by simply serializing an object – for instance,
// it's sometimes useful to merge two messages by simply concatenating them.
func NoMerge(buf []byte, descriptorSet *descriptor.FileDescriptorSet, rootPkg string, rootMsg string) error {
	msg := descriptorSet.GetMessage(rootPkg, rootMsg)
	if msg == nil {
		return &errUndefined{rootPkg, rootMsg, ""}
	}
	i := 0
	seen := make(map[int32]bool)
	for i < len(buf) {
		key, n, err := decodeVarint(buf, i)
		if err != nil {
			return err
		}
		i = i + n
		fieldNum := int32(key >> 3)
		wireType := int(key & 0x7)
		field := getFieldNumber(descriptorSet, rootPkg, rootMsg, msg, fieldNum)
		if field == nil || field.GetLabel() == descriptor.FieldDescriptorProto_LABEL_REPEATED {
			i, err = skip(buf, i, wireType)
			if err != nil {
				return err
			}
			continue
		}
		if seen[fieldNum] {
			return &errMerge{msg.GetName() + "." + field.GetName()}
		}
		seen[fieldNum] = true
		if !field.IsMessage() {
			i, err = skip(buf, i, wireType)
			if err != nil {
				return err
			}
			continue
		}
		length, n, err := decodeVarint(buf, i)
		if err != nil {
			return err
		}
		i += n
		fieldPkg, fieldMsg := descriptorSet.FindMessage(rootPkg, rootMsg, field.GetName())
		if len(fieldMsg) == 0 {
			return &errUndefined{rootPkg, rootMsg, field.GetName()}
		}
		err = NoMerge(buf[i:i+int(length)], descriptorSet, fieldPkg, fieldMsg)
		if err != nil {
			return err
		}
		i += int(length)
	}
	return nil
}
示例#3
0
文件: desc.go 项目: katydid/katydid
//NewDescriptorMap returns a map of the FileDescriptorSet starting at the message represented by the package name and message name.
func NewDescriptorMap(pkgName, msgName string, desc *descriptor.FileDescriptorSet) (DescMap, error) {
	root := desc.GetMessage(pkgName, msgName)
	if root == nil {
		return nil, &errUnknown{pkgName + "." + msgName}
	}
	d := &descMap{
		desc:       desc,
		root:       root,
		fieldToMsg: make(map[*descriptor.FieldDescriptorProto]*descriptor.DescriptorProto),
		msgToField: make(map[*descriptor.DescriptorProto]map[uint64]*descriptor.FieldDescriptorProto),
	}
	err := d.visit(pkgName, root)
	return d, err
}
示例#4
0
文件: merge.go 项目: gogo/fieldpath
func NoLatentAppendingOrMerging(buf []byte, descriptorSet *descriptor.FileDescriptorSet, rootPkg string, rootMsg string) error {
	msg := descriptorSet.GetMessage(rootPkg, rootMsg)
	if msg == nil {
		return &errUndefined{rootPkg, rootMsg, ""}
	}
	i := 0
	var lookingAt int32 = -1
	seen := make(map[int32]bool)
	for i < len(buf) {
		key, n, err := decodeVarint(buf, i)
		if err != nil {
			return err
		}
		i = i + n
		fieldNum := int32(key >> 3)
		wireType := int(key & 0x7)
		field := getFieldNumber(descriptorSet, rootPkg, rootMsg, msg, fieldNum)
		if seen[fieldNum] {
			return &errLatent{msg.GetName() + "." + field.GetName()}
		}
		if lookingAt != fieldNum {
			seen[lookingAt] = true
			lookingAt = fieldNum
		}
		if !field.IsMessage() {
			i, err = skip(buf, i, wireType)
			if err != nil {
				return err
			}
			continue
		}
		length, n, err := decodeVarint(buf, i)
		if err != nil {
			return err
		}
		i += n
		fieldPkg, fieldMsg := descriptorSet.FindMessage(rootPkg, rootMsg, field.GetName())
		if len(fieldMsg) == 0 {
			return &errUndefined{rootPkg, rootMsg, field.GetName()}
		}
		err = NoLatentAppendingOrMerging(buf[i:i+int(length)], descriptorSet, fieldPkg, fieldMsg)
		if err != nil {
			return err
		}
		i += int(length)
	}
	return nil
}
示例#5
0
func new(rootPackage string, rootMessage string, descSet *descriptor.FileDescriptorSet, path string) (*fdesc, error) {
	fieldpaths := strings.Split(path, ".")
	keys := make([]uint64, len(fieldpaths))
	fields := make([]*descriptor.FieldDescriptorProto, len(fieldpaths))

	curPackage := rootPackage
	curMessage := rootMessage
	last := len(fieldpaths) - 1
	var fieldDesc *descriptor.FieldDescriptorProto
	for i, f := range fieldpaths {
		fieldName := f
		fieldDesc = descSet.GetField(curPackage, curMessage, fieldName)
		if fieldDesc == nil {
			curPackage, fieldDesc = descSet.FindExtension(curPackage, curMessage, fieldName)
			if fieldDesc == nil {
				return nil, &errChild{fieldName: fieldName, pkg: curPackage, msg: curMessage}
			}
			typeNames := strings.Split(fieldDesc.GetTypeName(), ".")
			curMessage = fieldDesc.GetTypeName()
			if len(typeNames) > 1 {
				curPackage = typeNames[1]
				curMessage = typeNames[2]
			}
			fieldKey := fieldDesc.GetKeyUint64()
			keys[i] = fieldKey
			fields[i] = fieldDesc
		} else {
			fieldKey := fieldDesc.GetKeyUint64()
			if fieldDesc.IsMessage() {
				curPackage, curMessage = descSet.FindMessage(curPackage, curMessage, fieldName)
			} else if i != last {
				return nil, &errMessage{fieldName}
			}
			keys[i] = fieldKey
			fields[i] = fieldDesc
		}
	}
	fd := &fdesc{curPackage, curMessage, fields, fieldDesc, keys, 0}
	if fieldDesc.GetType() == descriptor.FieldDescriptorProto_TYPE_ENUM {
		typeNames := strings.Split(fieldDesc.GetTypeName(), ".")
		enumMessage := fieldDesc.GetTypeName()
		enumPackage := curPackage
		if len(typeNames) > 1 {
			enumPackage = typeNames[1]
			enumMessage = typeNames[2]
		}
		enum := descSet.GetEnum(enumPackage, enumMessage)
		if enum == nil {
			return nil, &errChild{fieldName: fieldDesc.GetName(), pkg: enumPackage, msg: enumMessage}
		}
		for _, v := range enum.GetValue() {
			if v.GetNumber() < fd.firstEnumValue {
				fd.firstEnumValue = v.GetNumber()
			}
		}
	}
	return fd, nil
}
示例#6
0
func toMessages(elems []string, fileDescriptorSet *descriptor.FileDescriptorSet, messages []string) ([]string, error) {
	if len(elems) < 2 {
		return nil, &errUndefinedMsg{elems[0], "???"}
	}
	msg := fileDescriptorSet.GetMessage(elems[0], elems[1])
	if msg == nil {
		return nil, &errUndefinedMsg{elems[0], elems[1]}
	}
	if len(elems) == 2 {
		messages = append(messages, elems[0]+"."+elems[1])
		return messages, nil
	}
	newPkg, newMsg := fileDescriptorSet.FindMessage(elems[0], elems[1], elems[2])
	if len(newMsg) == 0 {
		return nil, &errUndefined{elems[0], elems[1], elems[2]}
	}
	messages = append(messages, elems[0]+"."+elems[1])
	elems[1] = newPkg
	elems[2] = newMsg
	return toMessages(elems[1:], fileDescriptorSet, messages)
}
示例#7
0
//Collapses a proto fieldpath into a go fieldpath.  They are different if some of the fields in the fieldpath have been embedded.
func Collapse(rootPkg string, rootMsg string, path string, descriptorSet *descriptor.FileDescriptorSet) (string, error) {
	msg := descriptorSet.GetMessage(rootPkg, rootMsg)
	if msg == nil {
		return "", &errUndefined{rootPkg, rootMsg, path}
	}
	paths := strings.Split(path, ".")
	if len(paths) == 0 {
		return "", &errUndefined{rootPkg, rootMsg, path}
	}
	if len(paths) == 1 {
		return path, nil
	}
	for _, f := range msg.GetField() {
		if f.GetName() != paths[0] {
			continue
		}
		if f.IsMessage() {
			newRootPkg, newRootMsg := descriptorSet.FindMessage(rootPkg, rootMsg, f.GetName())
			if len(newRootPkg) == 0 || len(newRootMsg) == 0 {
				return "", &errUndefined{rootPkg, rootMsg, path}
			}
			newPath, err := Collapse(newRootPkg, newRootMsg, strings.Join(paths[1:], "."), descriptorSet)
			if err != nil {
				return "", err
			}
			if gogoproto.IsEmbed(f) {
				return newPath, nil
			} else {
				return paths[0] + "." + newPath, nil
			}
		}
	}
	if msg.IsExtendable() {
		newRootPkg, f := descriptorSet.FindExtension(rootPkg, rootMsg, paths[0])
		if f == nil {
			return "", &errUndefined{rootPkg, rootMsg, path}
		}
		typeName := f.GetTypeName()
		typeNames := strings.Split(typeName, ".")
		newRootMsg := typeName
		if len(typeNames) > 1 {
			newRootMsg = typeNames[len(typeNames)-1]
		}
		newPath, err := Collapse(newRootPkg, newRootMsg, strings.Join(paths[1:], "."), descriptorSet)
		if err != nil {
			return "", err
		}
		if gogoproto.IsEmbed(f) {
			return newPath, nil
		} else {
			return paths[0] + "." + newPath, nil
		}
	}
	return "", nil
}
示例#8
0
文件: form.go 项目: gpaul/letmegrpc
func getMessage(f *descriptor.FieldDescriptorProto, fileDescriptorSet *descriptor.FileDescriptorSet) *descriptor.DescriptorProto {
	typeNames := strings.Split(f.GetTypeName(), ".")
	packageName, messageName := typeNames[1], typeNames[2]
	return fileDescriptorSet.GetMessage(packageName, messageName)
}
示例#9
0
文件: form.go 项目: gpaul/letmegrpc
func getEnum(fileDescriptorSet *descriptor.FileDescriptorSet, f *descriptor.FieldDescriptorProto) *descriptor.EnumDescriptorProto {
	typeNames := strings.Split(f.GetTypeName(), ".")
	return fileDescriptorSet.GetEnum(typeNames[1], typeNames[2])
}
示例#10
0
func CreateCustom(methodName, packageName, messageName string, fileDescriptorSet *descriptor.FileDescriptorSet, buildField FieldBuilder) string {
	msg := fileDescriptorSet.GetMessage(packageName, messageName)
	proto3 := fileDescriptorSet.IsProto3(packageName, messageName)
	text := `
	<form class="form-horizontal">
	<div id="form"><div class="children"></div></div>
    <a href="#" id="submit" class="btn btn-primary" role="button">Submit</a>
    </form>
    `
	text += `
	<script>`
	text += Header
	text += `var nodeFactory = {` + strings.Join(BuilderMap(make(map[string]struct{}),
		"RootKeyword", false, msg, fileDescriptorSet), "\n") + `}
	`
	text += Builder(make(map[string]struct{}), true, "RootKeyword", false, msg, fileDescriptorSet, proto3, buildField)
	text += Init(methodName, "RootKeyword", false, msg)
	text += `
	init();

	</script>

	<style>

	.node{
		padding-left: 2em;
		min-height:20px;
	    padding:10px;
	    margin-top:10px;
	    margin-bottom:20px;
	    //border-left:0.5px solid #999;
	    -webkit-border-radius:4px;
	    -moz-border-radius:4px;
	    border-radius:4px;
	    -webkit-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05);
	    -moz-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05);
	    box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05);
	    background-color:#eaeaea;
	}

	.node .node {
		background-color:#e2e2e2;
	}

	.node .node .node {
		background-color:#d9d9d9;
	}

	.node .node .node .node {
		background-color:#d1d1d1;
	}

	.node .node .node .node .node {
		background-color:#c7c7c7;
	}

	.node .node .node .node .node .node {
		background-color:#c0c0c0;
	}

	label{
	        font-weight: normal;
	}

	.heading {
		font-weight: bold;
	}

	</style>
	`
	return text
}
示例#11
0
文件: string.go 项目: gogo/fieldpath
//Converts a marshalled protocol buffer to string.
//It does this pretty effienctly by not combining slices.
//It rather prints every element of the slice seperately.
//This is typically used for debugging.
func ToString(rootPkg string, rootMsg string, desc *descriptor.FileDescriptorSet, fpath string, buf []byte, tabs int, writer io.Writer) error {
	w := &stringWriter{tabs, writer}
	messagePkg := rootPkg
	messageName := rootMsg
	if len(fpath) > 0 {
		fd, err := new(rootPkg, rootMsg, desc, fpath)
		if err != nil {
			panic(err)
		}
		messagePkg = fd.rootPkg
		messageName = fd.rootMsg
	}
	msg := desc.GetMessage(messagePkg, messageName)
	if msg == nil {
		panic("message not found: " + messagePkg + "." + messageName)
	}
	offset := 0
	for offset < len(buf) {
		key, n, err := decodeVarint(buf, offset)
		if err != nil {
			panic(err)
		}
		offset += n
		fieldNum := int32(key >> 3)
		found := false
		for _, f := range msg.GetField() {
			if f.IsPacked() {
				panic("not implemented")
			}
			if f.GetNumber() != fieldNum {
				continue
			}
			found = true
			if f.GetType() == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
				length, nn, err := decodeVarint(buf, offset)
				if err != nil {
					panic(err)
				}
				offset += nn
				w.tab()
				w.String(f.GetName() + `:{` + "\n")
				err = ToString(messagePkg, messageName, desc, f.GetName(), buf[offset:offset+int(length)], tabs+1, writer)
				if err != nil {
					return err
				}
				offset += int(length)
				w.tab()
				w.String(`}` + "\n")
				break
			} else {
				w.tab()
				w.String(f.GetName() + `:`)
				var m int
				var err error
				switch f.GetType() {
				case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
					m, err = DecFloat64(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_FLOAT:
					m, err = DecFloat32(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_INT64:
					m, err = DecInt64(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_UINT64:
					m, err = DecUint64(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_INT32:
					m, err = DecInt32(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_FIXED64:
					m, err = DecFixed64(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_FIXED32:
					m, err = DecFixed32(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_BOOL:
					m, err = DecBool(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_STRING:
					m, err = DecString(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_GROUP:
					panic("not implemented")
				case descriptor.FieldDescriptorProto_TYPE_BYTES:
					m, err = DecBytes(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_UINT32:
					m, err = DecUint32(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_ENUM:
					m, err = DecInt32(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_SFIXED32:
					m, err = DecSfixed32(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_SFIXED64:
					m, err = DecSfixed64(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_SINT32:
					m, err = DecSint32(buf, offset, w)
				case descriptor.FieldDescriptorProto_TYPE_SINT64:
					m, err = DecSint64(buf, offset, w)
				default:
					panic("unreachable")
				}
				offset += m
				if err != nil {
					panic(err)
				}
				w.String(",\n")
				break
			}
		}
		if !found {
			for _, extRange := range msg.GetExtensionRange() {
				if extRange.GetStart() <= fieldNum && fieldNum <= extRange.GetEnd() {
					found = true
					break
				}
			}
			w.tab()
			offset -= n
			nn, err := protoSkip(buf[offset:])
			if err != nil {
				panic(err)
			}
			if !found {
				w.String("XXX_unrecognized:")
			} else {
				w.String("XXX_extensions:")
				extPkg, ext := desc.FindExtensionByFieldNumber(messagePkg, messageName, fieldNum)
				if ext != nil && ext.IsMessage() {
					names := strings.Split(ext.GetTypeName(), ".")
					if len(names) == 3 {
						_, nnn, err := decodeVarint(buf, offset+n)
						if err != nil {
							panic(err)
						}
						w.tab()
						w.String(extPkg + "." + names[2] + `:{` + "\n")
						err = ToString(extPkg, names[2], desc, "", buf[offset+n+nnn:offset+nn], tabs+1, writer)
						if err != nil {
							return err
						}
						offset += nn
						w.tab()
						w.String(`}` + "\n")
						break
					} else {
						panic(ext.GetTypeName())
					}
				}
			}
			w.Bytes(buf[offset : offset+nn])
			w.String("\n")
			offset += nn
		}
	}
	return nil
}