Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move openapi generator to deco tool #765

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions httpclient/fixtures/name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package fixtures

import (
"strings"
"unicode"
)

// These methods are taken from the OpenAPI code generator as it has been moved
// to a separate repository.

// Return the value of cond evaluated at the nearest letter to index i in name.
// dir determines the direction of search: if true, search forwards, if false,
// search backwards.
func search(name string, cond func(rune) bool, dir bool, i int) bool {
nameLen := len(name)
incr := 1
if !dir {
incr = -1
}
for j := i; j >= 0 && j < nameLen; j += incr {
if unicode.IsLetter(rune(name[j])) {
return cond(rune(name[j]))
}
}
return false
}

// Return the value of cond evaluated on the rune at index i in name. If that
// rune is not a letter, search in both directions for the nearest letter and
// return the result of cond on those letters.
func checkCondAtNearestLetters(name string, cond func(rune) bool, i int) bool {
r := rune(name[i])

if unicode.IsLetter(r) {
return cond(r)
}
return search(name, cond, true, i) && search(name, cond, false, i)
}

// emulate positive lookaheads from JVM regex:
// (?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|([-_\s])
// and convert all words to lower case

func splitASCII(name string) (w []string) {
var current []rune
nameLen := len(name)
var isPrevUpper, isCurrentUpper, isNextLower, isNextUpper, isNotLastChar bool
// we do assume here that all named entities are strictly ASCII
for i := 0; i < nameLen; i++ {
r := rune(name[i])
if r == '$' {
// we're naming language literals, $ is usually not allowed
continue
}
// if the current rune is a digit, check the neighboring runes to
// determine whether to treat this one as upper-case.
isCurrentUpper = checkCondAtNearestLetters(name, unicode.IsUpper, i)
r = unicode.ToLower(r)
isNextLower = false
isNextUpper = false
isNotLastChar = i+1 < nameLen
if isNotLastChar {
isNextLower = checkCondAtNearestLetters(name, unicode.IsLower, i+1)
isNextUpper = checkCondAtNearestLetters(name, unicode.IsUpper, i+1)
}
split, before, after := false, false, true
// At the end of a string of capital letters (e.g. HTML[P]arser).
if isPrevUpper && isCurrentUpper && isNextLower && isNotLastChar {
// (?<=[A-Z])(?=[A-Z][a-z])
split = true
before = false
after = true
}
// At the end of a camel- or pascal-case word (e.g. htm[l]Parser).
if !isCurrentUpper && isNextUpper {
// (?<=[a-z])(?=[A-Z])
split = true
before = true
after = false
}
if !unicode.IsLetter(r) && !unicode.IsNumber(r) {
// ([-_\s])
split = true
before = false
after = false
}
if before {
current = append(current, r)
}
if split && len(current) > 0 {
w = append(w, string(current))
current = []rune{}
}
if after {
current = append(current, r)
}
isPrevUpper = isCurrentUpper
}
if len(current) > 0 {
w = append(w, string(current))
}
return w
}

// PascalName creates NamesLikesThis
func pascalName(name string) string {
var sb strings.Builder
for _, w := range splitASCII(name) {
sb.WriteString(strings.Title(w))
}
return sb.String()
}
4 changes: 1 addition & 3 deletions httpclient/fixtures/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"fmt"
"net/http"
"net/url"

"github.com/databricks/databricks-sdk-go/openapi/code"
)

func resourceFromRequest(req *http.Request) string {
Expand Down Expand Up @@ -57,7 +55,7 @@ func bodyStub(req *http.Request) (string, error) {
// which is not something i'm willing to write on my weekend
expectedRequest += "ExpectedRequest: XXX {\n"
for key, value := range receivedRequest {
camel := (&code.Named{Name: key}).PascalName()
camel := pascalName(key)
expectedRequest += fmt.Sprintf("\t\t\t%s: %#v,\n", camel, value)
}
expectedRequest += "\t\t},\n"
Expand Down
Loading
Loading