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

Hyphenated file + include-as support #423

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
hyphenated files
  • Loading branch information
abhishekparwal committed Oct 8, 2019
commit 98a023e5a4ab6bf10369f38c53bae57be667e239
20 changes: 0 additions & 20 deletions ast/basetypeid_string.go

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

1 change: 1 addition & 0 deletions compile/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (c compiler) load(p string) (*Module, error) {
m := &Module{
Name: fileBaseName(p),
ThriftPath: p,
NormalizedThriftPath: filePathWithUnderscore(p),
Includes: make(map[string]*IncludedModule),
Constants: make(map[string]*Constant),
Types: make(map[string]TypeSpec),
Expand Down
1 change: 1 addition & 0 deletions compile/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ package compile
// The module name is usually just the basename of the ThriftPath.
type Module struct {
Name string
NormalizedThriftPath string
ThriftPath string

// Mapping from the /Thrift name/ to the compiled representation of
Expand Down
10 changes: 9 additions & 1 deletion compile/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ func capitalize(s string) string {
return string(unicode.ToUpper(x)) + string(s[i:])
}

// fileBaseName returns the base name of the given file without the extension.
func replaceHyphenWithUnderscore(str string) string {
return strings.ReplaceAll(str, "-", "_")
}

func filePathWithUnderscore(p string) string {
return filepath.Join(filepath.Dir(p), replaceHyphenWithUnderscore(filepath.Base(p)))
}

// fileBaseName returns the normalized base name of the given file without the extension.
func fileBaseName(p string) string {
return strings.TrimSuffix(filepath.Base(p), filepath.Ext(p))
}
Expand Down
13 changes: 12 additions & 1 deletion compile/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,25 @@ func TestFileBaseName(t *testing.T) {
tests := []struct{ input, output string }{
{"foo.bar", "foo"},
{"foo/bar.thrift", "bar"},
{"foo/bar-baz.thrift", "bar-baz"},
{"foo/bar-baz.thrift", "bar_baz"},
}

for _, tt := range tests {
assert.Equal(t, tt.output, fileBaseName(tt.input))
}
}

func TestFilePathBWithUnderscore(t *testing.T) {
tests := []struct{ input, output string }{
{"/home/abhishek.parwal/xyz.thrift", "/home/abhishek.parwal/xyz.thrift"},
{"/home/abhishek.parwal/bar-baz.thrift", "/home/abhishek.parwal/bar_baz.thrift"},
}

for _, tt := range tests {
assert.Equal(t, tt.output, filePathWithUnderscore(tt.input))
}
}

func TestSplitInclude(t *testing.T) {
tests := []struct {
input string
Expand Down
2 changes: 1 addition & 1 deletion gen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func generateModule(
// packageRelPath is foo/bar, and packageDir is $outputDir/foo/bar. All
// files for bar.thrift will be written to the $outputDir/foo/bar/ tree. The
// package will be importable via $importPrefix/foo/bar.
packageRelPath, err := i.RelativePackage(m.ThriftPath)
packageRelPath, err := i.RelativePackage(m.NormalizedThriftPath)
if err != nil {
return "", nil, err
}
Expand Down
48 changes: 48 additions & 0 deletions gen/internal/tests/thrift/type-defs.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
include "./structs.thrift"
include "./enums.thrift"

/**
* Number of seconds since epoch.
*
* Deprecated: Use ISOTime instead.
*/
typedef i64 Timestamp // alias of primitive
typedef string State

typedef i128 UUID // alias of struct

typedef list<Event> EventGroup // alias fo collection

struct i128 {
1: required i64 high
2: required i64 low
}

struct Event {
1: required UUID uuid // required typedef
2: optional Timestamp time // optional typedef
}

struct DefaultPrimitiveTypedef {
1: optional State state = "hello"
}

struct Transition {
1: required State fromState
2: required State toState
3: optional EventGroup events
}

typedef binary PDF // alias of []byte

typedef set<structs.Frame> FrameGroup

typedef map<structs.Point, structs.Point> PointMap

typedef set<binary> BinarySet

typedef map<structs.Edge, structs.Edge> EdgeMap

typedef map<State, i64> StateMap

typedef enums.EnumWithValues MyEnum
4 changes: 2 additions & 2 deletions gen/internal/tests/thrift/unions.thrift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
include "./typedefs.thrift"
include "./type-defs.thrift"

union EmptyUnion {}

union Document {
1: typedefs.PDF pdf
1: type-defs.PDF pdf
2: string plainText
}

Expand Down
2 changes: 1 addition & 1 deletion idl/internal/lex.rl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (lex *lexer) Lex(out *yySymType) int {
| ("'" ([^'\n\\] | '\\' any)* "'")
;

identifier = [a-zA-Z_] ([a-zA-Z0-9_] | '.' [a-zA-Z0-9_])*;
identifier = [a-zA-Z_-] ([a-zA-Z0-9_-] | '.' [a-zA-Z0-9_-])*;

integer = ('+' | '-')? digit+;
hex_integer = '0x' xdigit+;
Expand Down
20 changes: 0 additions & 20 deletions internal/envelope/envelopetest/client.go

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

20 changes: 0 additions & 20 deletions internal/envelope/envelopetest/server.go

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

20 changes: 0 additions & 20 deletions internal/envelope/exception/exception.go

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

20 changes: 0 additions & 20 deletions wire/type_string.go

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