Skip to content

Commit

Permalink
Merge pull request #77 from lestrrat/issue-75
Browse files Browse the repository at this point in the history
Fix message name generation
  • Loading branch information
jprobinson authored Mar 27, 2018
2 parents 1135d7a + d898d85 commit ce6ca8c
Show file tree
Hide file tree
Showing 7 changed files with 449 additions and 420 deletions.
816 changes: 408 additions & 408 deletions fixtures/kubernetes.proto

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion fixtures/most_popular-options.proto
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ message ArticleWithCountType {
message Media {
string caption = 1;
string copyright = 2;
message Media_metadata {
message MediaMetadata {
string format = 1;
int32 height = 2;
string url = 3;
Expand Down
2 changes: 1 addition & 1 deletion fixtures/most_popular.proto
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ message ArticleWithCountType {
message Media {
string caption = 1;
string copyright = 2;
message Media_metadata {
message MediaMetadata {
string format = 1;
int32 height = 2;
string url = 3;
Expand Down
4 changes: 2 additions & 2 deletions fixtures/semantic_api-options.proto
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ message GetNameConceptTypeSpecificConceptResponse {

message Concept {
repeated ConceptRelation ancestors = 1;
message Article_list {
message ArticleList {
message Result {
string body = 1;
string byline = 2;
Expand Down Expand Up @@ -144,7 +144,7 @@ message Concept {
string relation = 10;
}
repeated Link links = 12;
message Scope_note {
message ScopeNote {
string scope_note = 1;
string scope_note_name = 2;
string scope_note_type = 3;
Expand Down
4 changes: 2 additions & 2 deletions fixtures/semantic_api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ message GetNameConceptTypeSpecificConceptResponse {

message Concept {
repeated ConceptRelation ancestors = 1;
message Article_list {
message ArticleList {
message Result {
string body = 1;
string byline = 2;
Expand Down Expand Up @@ -143,7 +143,7 @@ message Concept {
string relation = 10;
}
repeated Link links = 12;
message Scope_note {
message ScopeNote {
string scope_note = 1;
string scope_note_name = 2;
string scope_note_type = 3;
Expand Down
2 changes: 1 addition & 1 deletion openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ func messageProtobuf(dst io.Writer, m *Model, defs map[string]*Items) {
}

// Now write this proper indentation
fmt.Fprintf(&b, "message %s {", m.Name)
fmt.Fprintf(&b, "message %s {", camelCase(m.Name))
writeLinesWithPrefix(&b, &buf, indentStr, true)
fmt.Fprintf(&b, "\n}")

Expand Down
39 changes: 34 additions & 5 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,39 @@ import (
"unicode"
)

// since we're not considering unicode here, we're not using unicode.*
func isAlphaNum(r rune) bool {
return (r >= 0x41 && r <= 0x5a) || // A-Z
(r >= 0x61 && r <= 0x7a) || // a-z
(r >= 0x30 && r <= 0x39) // 0-9
}

func camelCase(s string) string {
var first = true
var wasUnderscore bool
var buf bytes.Buffer
for _, r := range s {
// replace all non-alpha-numeric characters with an underscore
if !isAlphaNum(r) {
r = '_'
}

if r == '_' {
wasUnderscore = true
continue
}

if first || wasUnderscore {
r = unicode.ToUpper(r)
}
first = false
wasUnderscore = false
buf.WriteRune(r)
}

return buf.String()
}

func cleanSpacing(output []byte) []byte {
re := regexp.MustCompile(`}\n*message `)
output = re.ReplaceAll(output, []byte("}\n\nmessage "))
Expand Down Expand Up @@ -54,11 +87,7 @@ func cleanCharacters(input string) string {
for _, r := range input {
// anything other than a-z, A-Z, 0-9 should be converted
// to an underscore
switch {
case r >= 0x41 && r <= 0x5a: // A-Z
case r >= 0x61 && r <= 0x7a: // a-z
case r >= 0x30 && r <= 0x39: // 0-9
default:
if !isAlphaNum(r) {
r = '_'
}
buf.WriteRune(r)
Expand Down

0 comments on commit ce6ca8c

Please sign in to comment.