diff --git a/private/bufpkg/bufcheck/bufbreaking/internal/bufbreakingcheck/bufbreakingcheck.go b/private/bufpkg/bufcheck/bufbreaking/internal/bufbreakingcheck/bufbreakingcheck.go index 766d305763..e950502465 100644 --- a/private/bufpkg/bufcheck/bufbreaking/internal/bufbreakingcheck/bufbreakingcheck.go +++ b/private/bufpkg/bufcheck/bufbreaking/internal/bufbreakingcheck/bufbreakingcheck.go @@ -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" @@ -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 } @@ -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, ) } diff --git a/private/bufpkg/bufcheck/bufbreaking/internal/bufbreakingcheck/util.go b/private/bufpkg/bufcheck/bufbreaking/internal/bufbreakingcheck/util.go index c0d208d962..48e5fcb567 100644 --- a/private/bufpkg/bufcheck/bufbreaking/internal/bufbreakingcheck/util.go +++ b/private/bufpkg/bufcheck/bufbreaking/internal/bufbreakingcheck/util.go @@ -17,7 +17,6 @@ package bufbreakingcheck import ( "fmt" "sort" - "strconv" "strings" "github.com/bufbuild/buf/private/bufpkg/bufanalysis" @@ -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)) - } -} diff --git a/private/pkg/protodescriptor/protodescriptor.go b/private/pkg/protodescriptor/protodescriptor.go index ad0768fd0f..5c64958741 100644 --- a/private/pkg/protodescriptor/protodescriptor.go +++ b/private/pkg/protodescriptor/protodescriptor.go @@ -17,6 +17,7 @@ package protodescriptor import ( "errors" "fmt" + "strconv" "github.com/bufbuild/buf/private/pkg/normalpath" "google.golang.org/protobuf/proto" @@ -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)) + } +}