Skip to content

Commit

Permalink
Can't generate type or variable names starting with an integer (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriehSchneier authored May 20, 2024
1 parent 52734f3 commit 1e170f8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions codegen/arrai/auto/go.arrai
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ let sysl = //{./sysl};

let name =
let wordRE = //re.compile(`[\p{L}_][\p{L}\p{N}_]*`);
let trimRE = //re.compile(`^_+`).sub("");
# types starting with _ aren't exported so remove the underscores
# types can't start with an integer, if it does add an N to the start
let trimRE = \word
//re.compile(`^_+`).sub("", word) -> //re.compile(`^(\d)`).sub("N$1", .);
let initialisms = {
# https://github.com/golang/lint/blob/8f45f776aaf18cebc8d65861cc70c33c60471952/lint.go#L771
"ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML",
Expand All @@ -15,8 +18,8 @@ let name =
};
let uppers = \word
(wordRE.match(word) >> .(0)) -> \[first, ...rest] [trimRE(first)] ++ rest >> \v
let upper = //str.upper(trimRE(v));
cond {upper <: initialisms: upper, _: //str.title(v)};
let upper = //str.upper(trimRE(v));
cond {upper <: initialisms: upper, _: //str.title(v)};
\word
let result = cond word {"": "", _: //seq.concat(uppers(word))};
cond {result <: restricted: result++"_", _: result};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ func TestMiscellaneous_DoubleUnderscore(t *testing.T) {
_ = encoder_backend.Double_underscore{S: "accessible"}
}

func TestMiscellaneous_StartWithInteger(t *testing.T) {
t.Parallel()
// Just want to confirm that it generates a valid type
_ = encoder_backend.N1TypeStartsWithInt{N1s: "accessible"}
}

func TestMiscellaneous_TypesSomethingExternal(t *testing.T) {
t.Parallel()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ EncoderBackend [package="encoder_backend", ~vars_in_url_name]:
!type __double_underscore:
__s <: string

/start_with_int:
POST:
return ok <: _1TypeStartsWithInt

!type _1TypeStartsWithInt:
_1s <: string

0 comments on commit 1e170f8

Please sign in to comment.