Skip to content

Commit

Permalink
Improve ts/duration and handling of UPPER acronyms (#4)
Browse files Browse the repository at this point in the history
* Improve timestamp/duration and UPPER acronyms

* Use built version
  • Loading branch information
Alfus authored Mar 15, 2024
1 parent f2160f5 commit c81a2a6
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 88 deletions.
2 changes: 1 addition & 1 deletion cmd/protoc-gen-bigquery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (
)

func main() {
protoplugin.Main(protoplugin.HandlerFunc(pluginbigquery.Handle), protoplugin.WithVersion(protoschema.Version))
protoplugin.Main(protoplugin.HandlerFunc(pluginbigquery.Handle), protoplugin.WithVersion(protoschema.Version()))
}
2 changes: 1 addition & 1 deletion cmd/protoc-gen-jsonschema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (
)

func main() {
protoplugin.Main(protoplugin.HandlerFunc(pluginjsonschema.Handle), protoplugin.WithVersion(protoschema.Version))
protoplugin.Main(protoplugin.HandlerFunc(pluginjsonschema.Handle), protoplugin.WithVersion(protoschema.Version()))
}
2 changes: 1 addition & 1 deletion cmd/protoc-gen-pubsub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (
)

func main() {
protoplugin.Main(protoplugin.HandlerFunc(pluginpubsub.Handle), protoplugin.WithVersion(protoschema.Version))
protoplugin.Main(protoplugin.HandlerFunc(pluginpubsub.Handle), protoplugin.WithVersion(protoschema.Version()))
}
34 changes: 14 additions & 20 deletions internal/protoschema/jsonschema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p *jsonSchemaGenerator) generate(desc protoreflect.MessageDescriptor) {
result := make(map[string]interface{})
result["$schema"] = "https://json-schema.org/draft/2020-12/schema"
result["$id"] = p.getID(desc)
result["title"] = p.generateTitle(desc.Name())
result["title"] = generateTitle(desc.Name())
p.result[desc.FullName()] = result
if custom, ok := p.custom[desc.FullName()]; ok { // Custom generator.
custom(result, desc)
Expand Down Expand Up @@ -142,14 +142,16 @@ func (p *jsonSchemaGenerator) generateBoolValidation(_ protoreflect.FieldDescrip
entry["type"] = jsBoolean
}

func (p *jsonSchemaGenerator) generateTitle(name protoreflect.Name) string {
func generateTitle(name protoreflect.Name) string {
// Convert camel case to space separated words.
var result strings.Builder
for i, r := range name {
if unicode.IsUpper(r) && i > 0 {
for i, chr := range name {
isUpper := unicode.IsUpper(chr)
nextIsUpper := i+1 < len(name) && unicode.IsUpper(rune(name[i+1]))
if i > 0 && isUpper && !nextIsUpper {
result.WriteRune(' ')
}
result.WriteRune(r)
result.WriteRune(chr)
}
return result.String()
}
Expand All @@ -160,7 +162,7 @@ func (p *jsonSchemaGenerator) generateEnumValidation(field protoreflect.FieldDes
enum = append(enum, field.Enum().Values().Get(i).Name())
}
anyOf := []map[string]interface{}{
{"type": jsString, "enum": enum, "title": p.generateTitle(field.Enum().Name())},
{"type": jsString, "enum": enum, "title": generateTitle(field.Enum().Name())},
{"type": jsInteger, "minimum": math.MinInt32, "maximum": math.MaxInt32},
}
entry["anyOf"] = anyOf
Expand Down Expand Up @@ -229,21 +231,13 @@ func (p *jsonSchemaGenerator) makeWktGenerators() map[protoreflect.FullName]func
}
}

result["google.protobuf.Duration"] = func(result map[string]interface{}, desc protoreflect.MessageDescriptor) {
anyOf := []map[string]interface{}{
make(map[string]interface{}),
{"type": jsString, "format": "duration"},
}
p.generateDefault(anyOf[0], desc)
result["anyOf"] = anyOf
result["google.protobuf.Duration"] = func(result map[string]interface{}, _ protoreflect.MessageDescriptor) {
result["type"] = jsString
result["format"] = "duration"
}
result["google.protobuf.Timestamp"] = func(result map[string]interface{}, desc protoreflect.MessageDescriptor) {
anyOf := []map[string]interface{}{
make(map[string]interface{}),
{"type": jsString, "format": "date-time"},
}
p.generateDefault(anyOf[0], desc)
result["anyOf"] = anyOf
result["google.protobuf.Timestamp"] = func(result map[string]interface{}, _ protoreflect.MessageDescriptor) {
result["type"] = jsString
result["format"] = "date-time"
}

result["google.protobuf.Value"] = func(_ map[string]interface{}, _ protoreflect.MessageDescriptor) {}
Expand Down
11 changes: 11 additions & 0 deletions internal/protoschema/jsonschema/jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,14 @@ func TestJSONSchemaGolden(t *testing.T) {
}
}
}

func TestTitle(t *testing.T) {
t.Parallel()
require.Equal(t, "Foo", generateTitle("Foo"))
require.Equal(t, "Foo Bar", generateTitle("FooBar"))
require.Equal(t, "foo Bar", generateTitle("fooBar"))
require.Equal(t, "Foo Bar Baz", generateTitle("FooBarBaz"))
require.Equal(t, "FOO Bar", generateTitle("FOOBar"))
require.Equal(t, "U Int64 Value", generateTitle("UInt64Value"))
require.Equal(t, "Uint64 Value", generateTitle("Uint64Value"))
}
14 changes: 11 additions & 3 deletions internal/protoschema/protoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@

package protoschema

const (
// Version is the current version of protoschema which is shared by all plugins.
Version = "0.2.0-dev"
import (
"runtime/debug"
"strings"
)

func Version() string {
buildInfo, ok := debug.ReadBuildInfo()
if ok && buildInfo != nil && buildInfo.Main.Version != "" {
return strings.TrimSpace(buildInfo.Main.Version)
}
return "devel"
}
34 changes: 3 additions & 31 deletions internal/testdata/jsonschema/google.protobuf.Duration.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 3 additions & 31 deletions internal/testdata/jsonschema/google.protobuf.Timestamp.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c81a2a6

Please sign in to comment.