-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathrevision.go
33 lines (28 loc) · 907 Bytes
/
revision.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package apigee
import (
"strconv"
"strings"
"fmt"
)
// Revision represents a revision number. Edge returns rev numbers in string form.
// This marshals and unmarshals between that format and int.
type Revision int
// MarshalJSON implements the json.Marshaler interface. It marshals from
// a Revision holding an integer value like 2, into a string like "2".
func (r *Revision) MarshalJSON() ([]byte, error) {
rev := fmt.Sprintf("%d", r)
return []byte(rev), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface. It unmarshals from
// a string like "2" (including the quotes), into an integer 2.
func (r *Revision) UnmarshalJSON(b []byte) error {
rev, e := strconv.ParseInt(strings.TrimSuffix(strings.TrimPrefix(string(b),"\""),"\""), 10, 32)
if e != nil {
return e
}
*r = Revision(rev)
return nil
}
func (r Revision) String() string {
return fmt.Sprintf("%d", r)
}