Skip to content

Commit

Permalink
Add MustDecodeGTID()
Browse files Browse the repository at this point in the history
  • Loading branch information
enisoc committed Jul 28, 2014
1 parent 8206436 commit fc8bf9e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions go/vt/mysqlctl/proto/gtid.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ func DecodeGTID(s string) (GTID, error) {
return ParseGTID(parts[0], parts[1])
}

// MustDecodeGTID calls DecodeGTID and panics on error.
func MustDecodeGTID(s string) GTID {
gtid, err := DecodeGTID(s)
if err != nil {
panic(err)
}
return gtid
}

// GTIDField is a concrete struct that contains a GTID interface value. This can
// be used as a field inside marshalable structs, which cannot contain interface
// values because there would be no way to know which concrete type to
Expand Down
29 changes: 29 additions & 0 deletions go/vt/mysqlctl/proto/gtid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,35 @@ func TestDecodeGTID(t *testing.T) {
}
}

func TestMustDecodeGTID(t *testing.T) {
gtidParsers["flavorflav"] = func(s string) (GTID, error) {
return fakeGTID{value: s}, nil
}
input := "flavorflav/123-456:789"
want := fakeGTID{value: "123-456:789"}

got := MustDecodeGTID(input)
if got != want {
t.Errorf("DecodeGTID(%#v) = %#v, want %#v", input, got, want)
}
}

func TestMustDecodeGTIDError(t *testing.T) {
defer func() {
want := "ParseGTID: unknown flavor"
err := recover()
if err == nil {
t.Errorf("wrong error, got %#v, want %#v", err, want)
}
got, ok := err.(error)
if !ok || !strings.HasPrefix(got.Error(), want) {
t.Errorf("wrong error, got %#v, want %#v", got, want)
}
}()

MustDecodeGTID("unknown flavor !@$!@/yowzah")
}

func TestEncodeNilGTID(t *testing.T) {
input := GTID(nil)
want := ""
Expand Down

0 comments on commit fc8bf9e

Please sign in to comment.