Skip to content

Commit

Permalink
Expose pretty strings for FieldDescriptorProto enums (#1856)
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev authored Feb 23, 2023
1 parent 4cff71d commit 5c9e56d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strconv"
"strings"

"github.com/bufbuild/buf/private/pkg/protodescriptor"
"github.com/bufbuild/buf/private/pkg/protosource"
"github.com/bufbuild/buf/private/pkg/stringutil"
"google.golang.org/protobuf/types/descriptorpb"
Expand Down Expand Up @@ -262,7 +263,7 @@ func checkFieldSameLabel(add addFunc, corpus *corpus, previousField protosource.
// otherwise prints as hex
numberString := strconv.FormatInt(int64(field.Number()), 10)
// TODO: specific label location
add(field, nil, field.Location(), `Field %q on message %q changed label from %q to %q.`, numberString, field.Message().Name(), prettyPrintFieldDescriptorProtoLabel(previousField.Label()), prettyPrintFieldDescriptorProtoLabel(field.Label()))
add(field, nil, field.Location(), `Field %q on message %q changed label from %q to %q.`, numberString, field.Message().Name(), protodescriptor.FieldDescriptorProtoLabelPrettyString(previousField.Label()), protodescriptor.FieldDescriptorProtoLabelPrettyString(field.Label()))
}
return nil
}
Expand Down Expand Up @@ -493,8 +494,8 @@ func addFieldChangedType(add addFunc, previousField protosource.Field, field pro
`Field %q on message %q changed type from %q to %q.%s`,
previousNumberString,
field.Message().Name(),
prettyPrintFieldDescriptorProtoType(previousField.Type()),
prettyPrintFieldDescriptorProtoType(field.Type()),
protodescriptor.FieldDescriptorProtoTypePrettyString(previousField.Type()),
protodescriptor.FieldDescriptorProtoTypePrettyString(field.Type()),
combinedExtraMessage,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package bufbreakingcheck
import (
"fmt"
"sort"
"strconv"
"strings"

"github.com/bufbuild/buf/private/bufpkg/bufanalysis"
Expand Down Expand Up @@ -352,59 +351,3 @@ func withBackupLocation(primary protosource.Location, secondary protosource.Loca
}
return secondary
}

func prettyPrintFieldDescriptorProtoType(t descriptorpb.FieldDescriptorProto_Type) string {
switch t {
case descriptorpb.FieldDescriptorProto_TYPE_DOUBLE:
return "double"
case descriptorpb.FieldDescriptorProto_TYPE_FLOAT:
return "float"
case descriptorpb.FieldDescriptorProto_TYPE_INT64:
return "int64"
case descriptorpb.FieldDescriptorProto_TYPE_UINT64:
return "uint64"
case descriptorpb.FieldDescriptorProto_TYPE_INT32:
return "int32"
case descriptorpb.FieldDescriptorProto_TYPE_FIXED64:
return "fixed64"
case descriptorpb.FieldDescriptorProto_TYPE_FIXED32:
return "fixed32"
case descriptorpb.FieldDescriptorProto_TYPE_BOOL:
return "bool"
case descriptorpb.FieldDescriptorProto_TYPE_STRING:
return "string"
case descriptorpb.FieldDescriptorProto_TYPE_GROUP:
return "group"
case descriptorpb.FieldDescriptorProto_TYPE_MESSAGE:
return "message"
case descriptorpb.FieldDescriptorProto_TYPE_BYTES:
return "bytes"
case descriptorpb.FieldDescriptorProto_TYPE_UINT32:
return "uint32"
case descriptorpb.FieldDescriptorProto_TYPE_ENUM:
return "enum"
case descriptorpb.FieldDescriptorProto_TYPE_SFIXED32:
return "sfixed32"
case descriptorpb.FieldDescriptorProto_TYPE_SFIXED64:
return "sfixed64"
case descriptorpb.FieldDescriptorProto_TYPE_SINT32:
return "sint32"
case descriptorpb.FieldDescriptorProto_TYPE_SINT64:
return "sint64"
default:
return strconv.Itoa(int(t))
}
}

func prettyPrintFieldDescriptorProtoLabel(l descriptorpb.FieldDescriptorProto_Label) string {
switch l {
case descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL:
return "optional"
case descriptorpb.FieldDescriptorProto_LABEL_REQUIRED:
return "required"
case descriptorpb.FieldDescriptorProto_LABEL_REPEATED:
return "repeated"
default:
return strconv.Itoa(int(l))
}
}
61 changes: 61 additions & 0 deletions private/pkg/protodescriptor/protodescriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package protodescriptor
import (
"errors"
"fmt"
"strconv"

"github.com/bufbuild/buf/private/pkg/normalpath"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -223,3 +224,63 @@ func ValidateProtoPaths(name string, paths []string) error {
}
return nil
}

// FieldDescriptorProtoTypePrettyString prints a pretty string
// representation of the FieldDescriptorProto_Type.
func FieldDescriptorProtoTypePrettyString(t descriptorpb.FieldDescriptorProto_Type) string {
switch t {
case descriptorpb.FieldDescriptorProto_TYPE_DOUBLE:
return "double"
case descriptorpb.FieldDescriptorProto_TYPE_FLOAT:
return "float"
case descriptorpb.FieldDescriptorProto_TYPE_INT64:
return "int64"
case descriptorpb.FieldDescriptorProto_TYPE_UINT64:
return "uint64"
case descriptorpb.FieldDescriptorProto_TYPE_INT32:
return "int32"
case descriptorpb.FieldDescriptorProto_TYPE_FIXED64:
return "fixed64"
case descriptorpb.FieldDescriptorProto_TYPE_FIXED32:
return "fixed32"
case descriptorpb.FieldDescriptorProto_TYPE_BOOL:
return "bool"
case descriptorpb.FieldDescriptorProto_TYPE_STRING:
return "string"
case descriptorpb.FieldDescriptorProto_TYPE_GROUP:
return "group"
case descriptorpb.FieldDescriptorProto_TYPE_MESSAGE:
return "message"
case descriptorpb.FieldDescriptorProto_TYPE_BYTES:
return "bytes"
case descriptorpb.FieldDescriptorProto_TYPE_UINT32:
return "uint32"
case descriptorpb.FieldDescriptorProto_TYPE_ENUM:
return "enum"
case descriptorpb.FieldDescriptorProto_TYPE_SFIXED32:
return "sfixed32"
case descriptorpb.FieldDescriptorProto_TYPE_SFIXED64:
return "sfixed64"
case descriptorpb.FieldDescriptorProto_TYPE_SINT32:
return "sint32"
case descriptorpb.FieldDescriptorProto_TYPE_SINT64:
return "sint64"
default:
return strconv.Itoa(int(t))
}
}

// FieldDescriptorProtoLabelPrettyString prints a pretty string
// representation of the FieldDescriptorProto_Label.
func FieldDescriptorProtoLabelPrettyString(l descriptorpb.FieldDescriptorProto_Label) string {
switch l {
case descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL:
return "optional"
case descriptorpb.FieldDescriptorProto_LABEL_REQUIRED:
return "required"
case descriptorpb.FieldDescriptorProto_LABEL_REPEATED:
return "repeated"
default:
return strconv.Itoa(int(l))
}
}

0 comments on commit 5c9e56d

Please sign in to comment.