Example #1
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
}
Example #2
0
func GetMap(file *descriptor.FileDescriptorProto, field *descriptor.FieldDescriptorProto) *descriptor.DescriptorProto {
	if !field.IsMessage() {
		return nil
	}
	typeName := field.GetTypeName()
	if strings.Contains(typeName, "Map") && !strings.HasSuffix(typeName, "Entry") {
		typeName += "." + CamelCase(field.GetName()) + "Entry"
	}
	ts := strings.Split(typeName, ".")
	if len(ts) == 1 {
		return file.GetMessage(typeName)
	}
	newTypeName := strings.Join(ts[2:], ".")
	return file.GetMessage(newTypeName)
}