forked from bsm/openrtb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
158 additions
and
56 deletions.
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
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
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,53 @@ | ||
package openrtb | ||
|
||
import ( | ||
"encoding/json" | ||
"strconv" | ||
) | ||
|
||
// NumberOrString attempts to fix OpenRTB incompatibilities | ||
// of exchanges. On decoding, it can handle numbers and strings. | ||
// On encoding, it will generate a number, as intended by the | ||
// standard. | ||
type NumberOrString int | ||
|
||
// UnmarshalJSON implements json.Unmarshaler | ||
func (n *NumberOrString) UnmarshalJSON(data []byte) (err error) { | ||
var v int | ||
|
||
if len(data) > 2 && data[0] == '"' && data[len(data)-1] == '"' { | ||
err = json.Unmarshal(data[1:len(data)-1], &v) | ||
} else { | ||
err = json.Unmarshal(data, &v) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*n = NumberOrString(v) | ||
return nil | ||
} | ||
|
||
// StringOrNumber attempts to fix OpenRTB incompatibilities | ||
// of exchanges. On decoding, it can handle numbers and strings. | ||
// On encoding, it will generate a string, as intended by the | ||
// standard. | ||
type StringOrNumber string | ||
|
||
// UnmarshalJSON implements json.Unmarshaler | ||
func (n *StringOrNumber) UnmarshalJSON(data []byte) error { | ||
if len(data) > 2 && data[0] == '"' && data[len(data)-1] == '"' { | ||
var v string | ||
if err := json.Unmarshal(data, &v); err != nil { | ||
return err | ||
} | ||
*n = StringOrNumber(v) | ||
} else { | ||
var v int | ||
if err := json.Unmarshal(data, &v); err != nil { | ||
return err | ||
} | ||
*n = StringOrNumber(strconv.Itoa(v)) | ||
} | ||
return nil | ||
} |
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,54 @@ | ||
package openrtb | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("NumberOrString", func() { | ||
|
||
It("should decode numbers", func() { | ||
var n NumberOrString | ||
Expect(json.Unmarshal([]byte(`33`), &n)).To(Succeed()) | ||
Expect(n).To(Equal(NumberOrString(33))) | ||
}) | ||
|
||
It("should decode strings", func() { | ||
var n NumberOrString | ||
Expect(json.Unmarshal([]byte(`"33"`), &n)).To(Succeed()) | ||
Expect(n).To(Equal(NumberOrString(33))) | ||
}) | ||
|
||
It("should encode to numbers", func() { | ||
var n NumberOrString = 33 | ||
bin, err := json.Marshal(n) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(string(bin)).To(Equal(`33`)) | ||
}) | ||
|
||
}) | ||
|
||
var _ = Describe("StringOrNumber", func() { | ||
|
||
It("should decode numbers", func() { | ||
var n StringOrNumber | ||
Expect(json.Unmarshal([]byte(`33`), &n)).To(Succeed()) | ||
Expect(n).To(Equal(StringOrNumber("33"))) | ||
}) | ||
|
||
It("should decode strings", func() { | ||
var n StringOrNumber | ||
Expect(json.Unmarshal([]byte(`"33"`), &n)).To(Succeed()) | ||
Expect(n).To(Equal(StringOrNumber("33"))) | ||
}) | ||
|
||
It("should encode to strings", func() { | ||
var n StringOrNumber = "33" | ||
bin, err := json.Marshal(n) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(string(bin)).To(Equal(`"33"`)) | ||
}) | ||
|
||
}) |