-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from andygrunwald/fix_patchset_number
Fix type of EventInfo.PatchSet.Number
- Loading branch information
Showing
3 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |