This repository has been archived by the owner on Oct 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use RawJSON type for Ext instead of interface{}
As proposed in: #8
- Loading branch information
Showing
25 changed files
with
106 additions
and
28 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
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
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
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,25 @@ | ||
package openrtb | ||
|
||
import "errors" | ||
|
||
// RawJSON is a raw encoded JSON value. | ||
// It implements encoding/json.Marshaler and encoding/json.Unmarshaler and can | ||
// be used to delay JSON decoding or precompute a JSON encoding. | ||
// | ||
// Basically, it's just a copy of encoding/json.RawMessage type, | ||
// but with more convenient non-pointer encoding. | ||
type RawJSON []byte | ||
|
||
// MarshalJSON returns m as the JSON encoding of m. | ||
func (m RawJSON) MarshalJSON() ([]byte, error) { | ||
return m, nil | ||
} | ||
|
||
// UnmarshalJSON sets *m to a copy of data. | ||
func (m *RawJSON) UnmarshalJSON(data []byte) error { | ||
if m == nil { | ||
return errors.New("openrtb.RawJSON: UnmarshalJSON on nil pointer") | ||
} | ||
*m = append((*m)[0:0], data...) | ||
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,57 @@ | ||
package openrtb_test | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/mxmCherry/openrtb" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("RawJSON", func() { | ||
|
||
var _ json.Marshaler = (openrtb.RawJSON)(nil) | ||
var _ json.Unmarshaler = (*openrtb.RawJSON)(nil) | ||
|
||
It("should encode JSON", func() { | ||
subject := openrtb.RawJSON(`true`) | ||
|
||
actual, err := subject.MarshalJSON() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(actual).To(Equal([]byte(`true`))) | ||
}) | ||
|
||
It("should decode JSON", func() { | ||
subject := openrtb.RawJSON(nil) | ||
|
||
err := subject.UnmarshalJSON([]byte(`true`)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(subject).To(Equal(openrtb.RawJSON(`true`))) | ||
}) | ||
|
||
It("should decode JSON when embedded into struct", func() { | ||
wrapper := struct { | ||
Raw openrtb.RawJSON `json:"raw"` | ||
}{ | ||
Raw: nil, | ||
} | ||
|
||
err := json.Unmarshal([]byte(`{"raw":true}`), &wrapper) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(wrapper.Raw).To(Equal(openrtb.RawJSON(`true`))) | ||
}) | ||
|
||
It("should encode JSON when embedded into struct", func() { | ||
wrapper := struct { | ||
Raw openrtb.RawJSON `json:"raw"` | ||
}{ | ||
Raw: openrtb.RawJSON(`true`), | ||
} | ||
|
||
actual, err := json.Marshal(wrapper) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(actual).To(MatchJSON(`{"raw":true}`)) | ||
}) | ||
|
||
}) |
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
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