Skip to content

Commit

Permalink
Add support for changed types in thriftbreak (#559)
Browse files Browse the repository at this point in the history
* Add check for changed types

* Update error message

* Update struct name
  • Loading branch information
moisesvega authored Oct 10, 2022
1 parent 183888f commit e6d71ae
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
17 changes: 17 additions & 0 deletions internal/compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ func (p *Pass) requiredField(fromField, toField *compile.FieldSpec, to *compile.
}
}

func (p *Pass) changedTypes(fromField, toField *compile.FieldSpec, to *compile.StructSpec, file string) {
if fromField.Type == nil || toField.Type == nil {
return
}

if fromField.Type.ThriftName() != toField.Type.ThriftName() {
p.Report(Diagnostic{
FilePath: file,
Message: fmt.Sprintf(
"changing type of field %q in struct %q from %q to %q",
toField.ThriftName(), to.ThriftName(), fromField.Type.ThriftName(),
toField.Type.ThriftName()),
})
}
}

// StructSpecs compares two structs defined in a Thrift file.
func (p *Pass) structSpecs(from, to *compile.StructSpec, file string) {
fields := make(map[int16]*compile.FieldSpec, len(from.Fields))
Expand All @@ -109,6 +125,7 @@ func (p *Pass) structSpecs(from, to *compile.StructSpec, file string) {
for _, toField := range to.Fields {
if fromField, ok := fields[toField.ID]; ok {
p.requiredField(fromField, toField, to, file)
p.changedTypes(fromField, toField, to, file)
} else if toField.Required {
p.Report(Diagnostic{
FilePath: file,
Expand Down
26 changes: 26 additions & 0 deletions internal/compare/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@ func TestErrorRequiredCase(t *testing.T) {
wantError: `foo.thrift:changing an optional field "fieldA" in "structA" to required` +
"\n" + `foo.thrift:changing an optional field "fieldB" in "structA" to required`,
},
{
desc: "found a type changed",
fromStruct: &compile.StructSpec{
Name: "structA",
Fields: compile.FieldGroup{
&compile.FieldSpec{
Name: "fieldA",
Type: &compile.BinarySpec{},
},
},
},
toStruct: &compile.StructSpec{
Name: "structA",
Fields: compile.FieldGroup{
&compile.FieldSpec{
Name: "fieldA",
Type: &compile.BoolSpec{},
},
&compile.FieldSpec{
Name: "fieldB",
Type: &compile.BinarySpec{},
},
},
},
wantError: `foo.thrift:changing type of field "fieldA" in struct "structA" from "binary" to "bool"`,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit e6d71ae

Please sign in to comment.