Exemple #1
0
func fieldElmType(inField *descriptor.FieldDescriptorProto) string {
	switch inField.GetType() {
	case descriptor.FieldDescriptorProto_TYPE_INT32,
		descriptor.FieldDescriptorProto_TYPE_INT64,
		descriptor.FieldDescriptorProto_TYPE_UINT32,
		descriptor.FieldDescriptorProto_TYPE_UINT64,
		descriptor.FieldDescriptorProto_TYPE_SINT32,
		descriptor.FieldDescriptorProto_TYPE_SINT64,
		descriptor.FieldDescriptorProto_TYPE_FIXED32,
		descriptor.FieldDescriptorProto_TYPE_FIXED64,
		descriptor.FieldDescriptorProto_TYPE_SFIXED32,
		descriptor.FieldDescriptorProto_TYPE_SFIXED64:
		return "Int"
	case descriptor.FieldDescriptorProto_TYPE_FLOAT,
		descriptor.FieldDescriptorProto_TYPE_DOUBLE:
		return "Float"
	case descriptor.FieldDescriptorProto_TYPE_BOOL:
		return "Bool"
	case descriptor.FieldDescriptorProto_TYPE_STRING:
		return "String"
	case descriptor.FieldDescriptorProto_TYPE_ENUM:
		// XXX
		return elmFieldType(inField)
	case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
		// XXX
		return elmFieldType(inField)
	case descriptor.FieldDescriptorProto_TYPE_BYTES:
		// XXX
		return "Bytes"
	default:
		// TODO: Return error.
		return fmt.Sprintf("Error generating type for field %q %s", inField.GetName(), inField.GetType())
	}
}
func (p *protoBufErrors) lintProtoField(
	pathIndex int32,
	parentPath []int32,
	messageField *descriptor.FieldDescriptorProto,
) {
	path := append(
		parentPath,
		pathMessageField,
		pathIndex,
	)
	if !isLowerUnderscore(messageField.GetName()) {
		p.addError(&protoBufError{
			path:        path,
			errorCode:   errorFieldCase,
			errorString: messageField.GetName(),
		})
	}
}
Exemple #3
0
func fillTreeWithField(tree *tree, parent string, proto *descriptor.FieldDescriptorProto, loc string, locs map[string]*descriptor.SourceCodeInfo_Location) *field {
	key := fmt.Sprintf("%s.%s", parent, proto.GetName())
	tree.fields[key] = &field{key: key, comment: getComment(loc, locs), FieldDescriptorProto: proto}
	if proto.GetLabel() == descriptor.FieldDescriptorProto_LABEL_REPEATED {
		tree.fields[key].repeated = true
	}
	if proto.OneofIndex != nil {
		if parent, ok := tree.messages[parent]; ok {
			for _, oneof := range parent.oneofs {
				if oneof.index == proto.GetOneofIndex() {
					oneof.fields = append(oneof.fields, tree.fields[key])
					tree.fields[key].isOneOf = true
				}
			}
		}
	}
	return tree.fields[key]
}
func convertField(curPkg *ProtoPackage, desc *descriptor.FieldDescriptorProto, msg *descriptor.DescriptorProto) (*Field, error) {
	field := &Field{
		Name: desc.GetName(),
	}

	switch desc.GetType() {
	case descriptor.FieldDescriptorProto_TYPE_DOUBLE,
		descriptor.FieldDescriptorProto_TYPE_FLOAT:
		field.Type = "FLOAT"

	case descriptor.FieldDescriptorProto_TYPE_INT64,
		descriptor.FieldDescriptorProto_TYPE_UINT64,
		descriptor.FieldDescriptorProto_TYPE_INT32,
		descriptor.FieldDescriptorProto_TYPE_UINT32,
		descriptor.FieldDescriptorProto_TYPE_FIXED64,
		descriptor.FieldDescriptorProto_TYPE_FIXED32,
		descriptor.FieldDescriptorProto_TYPE_SFIXED32,
		descriptor.FieldDescriptorProto_TYPE_SFIXED64,
		descriptor.FieldDescriptorProto_TYPE_SINT32,
		descriptor.FieldDescriptorProto_TYPE_SINT64:
		field.Type = "INTEGER"

	case descriptor.FieldDescriptorProto_TYPE_STRING,
		descriptor.FieldDescriptorProto_TYPE_BYTES,
		descriptor.FieldDescriptorProto_TYPE_ENUM:
		field.Type = "STRING"

	case descriptor.FieldDescriptorProto_TYPE_BOOL:
		field.Type = "BOOLEAN"

	case descriptor.FieldDescriptorProto_TYPE_GROUP,
		descriptor.FieldDescriptorProto_TYPE_MESSAGE:
		field.Type = "RECORD"

	default:
		return nil, fmt.Errorf("unrecognized field type: %s", desc.GetType().String())
	}

	switch desc.GetLabel() {
	case descriptor.FieldDescriptorProto_LABEL_OPTIONAL:
		field.Mode = "NULLABLE"

	case descriptor.FieldDescriptorProto_LABEL_REQUIRED:
		field.Mode = "REQUIRED"

	case descriptor.FieldDescriptorProto_LABEL_REPEATED:
		field.Mode = "REPEATED"

	default:
		return nil, fmt.Errorf("unrecognized field label: %s", desc.GetLabel().String())
	}

	if field.Type != "RECORD" {
		return field, nil
	}

	recordType, ok := curPkg.lookupType(desc.GetTypeName())
	if !ok {
		return nil, fmt.Errorf("no such message type named %s", desc.GetTypeName())
	}
	var err error
	field.Fields, err = convertMessageType(curPkg, recordType)
	if err != nil {
		return nil, err
	}

	return field, nil
}