Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (Un)Marshal Text/Binary methods to support json parsing (#670) #678

Merged
merged 1 commit into from
Aug 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions encoding/json/encoding_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package json_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestJsonEncoding(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Json Encoding Suite")
}
73 changes: 73 additions & 0 deletions encoding/json/encoding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package json_test

import (
"bytes"
"encoding/json"
"fmt"

"github.com/goadesign/goa/uuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("JsonEncoding", func() {

Describe("handle goa/uuid/UUID", func() {
name := "Test"
id, _ := uuid.FromString("c0586f01-87b5-462b-a673-3b2dcf619091")

type Payload struct {
ID uuid.UUID
Name string
}

It("encode", func() {
data := Payload{
id,
name,
}

var b bytes.Buffer
encoder := json.NewEncoder(&b)
encoder.Encode(data)
s := b.String()

Ω(s).Should(ContainSubstring(id.String()))
Ω(s).Should(ContainSubstring(name))
})

It("decode", func() {
encoded := fmt.Sprintf(`{"ID":"%s","Name":"%s"}`, id, name)

var payload Payload
var b bytes.Buffer
b.WriteString(encoded)

decoder := json.NewDecoder(&b)
decoder.Decode(&payload)

Ω(payload.ID.String()).Should(Equal(id.String()))
Ω(payload.Name).Should(Equal(name))
})

It("round trip", func() {
data := Payload{
id,
name,
}

var payload Payload
var b bytes.Buffer

encoder := json.NewEncoder(&b)
encoder.Encode(data)

decoder := json.NewDecoder(&b)
decoder.Decode(&payload)

Ω(payload.ID.String()).Should(Equal(data.ID.String()))
Ω(payload.Name).Should(Equal(data.Name))
})

})
})
36 changes: 31 additions & 5 deletions uuid/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,37 @@ func NewV4() UUID {
return UUID(uuid.NewV4())
}

// Used in string method conversion
const dash byte = '-'

// Returns canonical string representation of UUID:
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
// String Wrapper over the real String method
func (u UUID) String() string {
return uuid.UUID(u).String()
}

// MarshalText Wrapper over the real MarshalText method
func (u UUID) MarshalText() (text []byte, err error) {
return uuid.UUID(u).MarshalText()
}

// MarshalBinary Wrapper over the real MarshalBinary method
func (u UUID) MarshalBinary() ([]byte, error) {
return uuid.UUID(u).MarshalBinary()
}

// UnmarshalBinary Wrapper over the real UnmarshalBinary method
func (u *UUID) UnmarshalBinary(data []byte) error {
t := uuid.UUID{}
err := t.UnmarshalBinary(data)
for i, b := range t.Bytes() {
u[i] = b
}
return err
}

// UnmarshalText Wrapper over the real UnmarshalText method
func (u *UUID) UnmarshalText(text []byte) error {
t := uuid.UUID{}
err := t.UnmarshalText(text)
for i, b := range t.Bytes() {
u[i] = b
}
return err
}