Skip to content

Commit

Permalink
Merge pull request #37 from andygrunwald/fix_patchset_number
Browse files Browse the repository at this point in the history
Fix type of EventInfo.PatchSet.Number
  • Loading branch information
andygrunwald authored Jun 5, 2017
2 parents 73dc271 + 16138b0 commit 2218433
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion events.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
//
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/json.html#patchSet
type PatchSet struct {
Number string `json:"number"`
Number Number `json:"number"`
Revision string `json:"revision"`
Parents []string `json:"parents"`
Ref string `json:"ref"`
Expand Down
42 changes: 42 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gerrit

import (
"encoding/json"
"errors"
"strconv"
)


// Number is a string representing a number. This type is only used in cases
// where the API being queried may return an inconsistent result.
type Number string

// String returns the string representing the current number.
func (n *Number) String() string {
return string(*n)
}

// Int returns the current number as an integer
func (n *Number) Int() (int, error) {
return strconv.Atoi(n.String())
}

func (n *Number) UnmarshalJSON(data []byte) error {
// `data` is a number represented as a string (ex. "5").
var stringNumber string
if err := json.Unmarshal(data, &stringNumber); err == nil {
*n = Number(stringNumber)
return nil
}

// `data` is a number represented as an integer (ex. 5). Here
// we're using json.Unmarshal to convert bytes -> number which
// we then convert to our own Number type.
var number int
if err := json.Unmarshal(data, &number); err == nil {
*n = Number(strconv.Itoa(number))
return nil
}
return errors.New("Cannot convert data to number")
}

49 changes: 49 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package gerrit_test

import (
"encoding/json"
"testing"
"github.com/andygrunwald/go-gerrit"
)

func TestTypesNumber_String(t *testing.T) {
number := gerrit.Number("7")
if number.String() != "7" {
t.Fatalf("%s != 7", number.String())
}
}

func TestTypesNumber_Int(t *testing.T) {
number := gerrit.Number("7")
integer, err := number.Int()
if err != nil {
t.Fatal(err)
}
if integer != 7 {
t.Fatalf("%d != 7", integer)
}
}

func TestTypesNumber_UnmarshalJSON_String(t *testing.T) {
var number gerrit.Number
if err := json.Unmarshal([]byte(`"7"`), &number); err != nil {
t.Fatal(err)
}
if number.String() != "7" {
t.Fatalf("%s != 7", number.String())
}
}

func TestTypesNumber_UnmarshalJSON_Int(t *testing.T) {
var number gerrit.Number
if err := json.Unmarshal([]byte("7"), &number); err != nil {
t.Fatal(err)
}
integer, err := number.Int()
if err != nil {
t.Fatal(err)
}
if integer != 7 {
t.Fatalf("%d != 7", integer)
}
}

0 comments on commit 2218433

Please sign in to comment.