Skip to content

Commit

Permalink
Forbid generating code with thriftrw reserved identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
bombela committed Nov 10, 2016
1 parent 04568db commit ac7cb53
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
38 changes: 34 additions & 4 deletions gen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import (
"go.uber.org/thriftrw/compile"
)

var reservedIdentifiers = map[string]struct{}{
"ToWire": {},
"FromWire": {},
"String": {},
}

// fieldGroupGenerator is responsible for generating code for FieldGroups.
type fieldGroupGenerator struct {
Namespace
Expand All @@ -37,6 +43,18 @@ type fieldGroupGenerator struct {
// must be set for it to be valid.
IsUnion bool
AllowEmptyUnion bool

// This field group represents a Thrift exception.
IsException bool
}

func (f fieldGroupGenerator) checkReservedIdentifier(name string) error {
_, match := reservedIdentifiers[name]
match = match || (f.IsException && name == "Error")
if match {
return fmt.Errorf("%q is a reserved ThriftRW identifier", name)
}
return nil
}

func (f fieldGroupGenerator) Generate(g Generator) error {
Expand Down Expand Up @@ -97,11 +115,23 @@ func (f *fieldGroupGenerator) declFieldName(fs *compile.FieldSpec) (string, erro
if err != nil {
return "", err
}
if err := f.Reserve(name); err != nil {
if fromAnnotation {
return "", fmt.Errorf("could not declare field %q (from go.name annotation): %v", name, err)

if err = f.checkReservedIdentifier(name); err == nil {
err = f.Reserve(name)
}

if err != nil {
originalName := (name == fs.ThriftName())
var note string
switch {
case originalName && fromAnnotation:
note = "(from go.name annotation)"
case !originalName && fromAnnotation:
note = fmt.Sprintf(" (from %q go.name annotation): %v", fs.ThriftName())
case !originalName && !fromAnnotation:
note = fmt.Sprintf(" (from %q): %v", fs.ThriftName())
}
return "", fmt.Errorf("could not declare field %q (from %q): %v", name, fs.ThriftName(), err)
return "", fmt.Errorf("could not declare field %q%s: %v", name, note, err)
}
return name, nil
}
Expand Down
9 changes: 5 additions & 4 deletions gen/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ func structure(g Generator, spec *compile.StructSpec) error {
}

fg := fieldGroupGenerator{
Namespace: NewNamespace(),
Name: name,
Fields: spec.Fields,
IsUnion: spec.Type == ast.UnionType,
Namespace: NewNamespace(),
Name: name,
Fields: spec.Fields,
IsUnion: spec.Type == ast.UnionType,
IsException: spec.Type == ast.ExceptionType,
}

if err := fg.Generate(g); err != nil {
Expand Down

0 comments on commit ac7cb53

Please sign in to comment.