Skip to content

Commit

Permalink
Use a bool on the GoGenerator struct for SignedBytes instruction.
Browse files Browse the repository at this point in the history
Updated the README to reflect the new help message.

Added a test file in a new subdirectory to prevent other generator
tests from failing. And added a new test to only target the
SignedBytes feature.
  • Loading branch information
Coleman McFarland committed Jan 9, 2016
1 parent 482b032 commit a07debc
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,17 @@ How to use the generator:
$ go install github.com/samuel/go-thrift/generator

$ generator --help
Usage of parsimony: [options] inputfile outputpath
-go.binarystring=false: Always use string for binary instead of []byte
-go.json.enumnum=false: For JSON marshal enums by number instead of name
-go.pointers=false: Make all fields pointers
Usage of generator:
-go.binarystring
Always use string for binary instead of []byte
-go.importprefix string
Prefix for Thrift-generated go package imports
-go.json.enumnum
For JSON marshal enums by number instead of name
-go.pointers
Make all fields pointers
-go.signedbytes
Interpret Thrift byte as Go signed int8 type

$ generator cassandra.thrift $GOPATH/src/

Expand Down
3 changes: 2 additions & 1 deletion generator/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type GoGenerator struct {
Packages map[string]GoPackage
Format bool
Pointers bool
SignedBytes bool
}

var goKeywords = map[string]bool{
Expand Down Expand Up @@ -170,7 +171,7 @@ func (g *GoGenerator) formatType(pkg string, thrift *parser.Thrift, typ *parser.
case "bool", "string":
return ptr + typ.Name
case "byte":
if *flagGoSignedBytes {
if g.SignedBytes {
return ptr + "int8"
}
return ptr + typ.Name
Expand Down
34 changes: 34 additions & 0 deletions generator/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,40 @@ func TestSimple(t *testing.T) {
}
}

func TestFlagGoSignedBytes(t *testing.T) {
files, err := filepath.Glob("../testfiles/generator/withFlags/go.signedbytes/*.thrift")
if err != nil {
t.Fatal(err)
}

outPath, err := ioutil.TempDir("", "go-thrift-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(outPath)

p := &parser.Parser{}
for _, fn := range files {
t.Logf("Testing %s", fn)
th, _, err := p.ParseFile(fn)
if err != nil {
t.Fatalf("Failed to parse %s: %s", fn, err)
}
generator := &GoGenerator{
ThriftFiles: th,
Format: true,
Pointers: false,
SignedBytes: true,
}
if err := generator.Generate(outPath); err != nil {
t.Fatalf("Failed to generate go for %s: %s", fn, err)
}
base := fn[:len(fn)-len(".thrift")]
name := filepath.Base(base)
compareFiles(t, outPath+"/gentest/"+name+".go", base+".go")
}
}

func compareFiles(t *testing.T, actualPath, expectedPath string) {
ac, err := ioutil.ReadFile(actualPath)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func main() {
generator := &GoGenerator{
ThriftFiles: parsedThrift,
Format: true,
SignedBytes: *flagGoSignedBytes,
}
err = generator.Generate(outpath)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions testfiles/generator/withFlags/go.signedbytes/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This file is automatically generated. Do not modify.

package gentest

import (
"fmt"
)

var _ = fmt.Sprintf

type ByteAndListByte struct {
AByte int8 `thrift:"1,required" json:"a_byte"`
AListByte []int8 `thrift:"2,required" json:"a_list_byte"`
}
6 changes: 6 additions & 0 deletions testfiles/generator/withFlags/go.signedbytes/bytes.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace go gentest

struct ByteAndListByte {
1: byte a_byte,
2: list<byte> a_list_byte
}

0 comments on commit a07debc

Please sign in to comment.