Skip to content

Commit 40796f0

Browse files
committed
impl Stringer for struct and typed values
1 parent 5b5c648 commit 40796f0

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

Diff for: struct.go

+9
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ func (v Struct) MarshalJSON() ([]byte, error) {
143143
return json.Marshal(raw)
144144
}
145145

146+
// String returns the struct in its JSON representation.
147+
func (v Struct) String() string {
148+
data, err := v.MarshalJSON()
149+
if err != nil {
150+
return fmt.Sprintf(`{"error": %v}`, err)
151+
}
152+
return string(data)
153+
}
154+
146155
// Generate a random struct field. This method is implemented for use in quick
147156
// tests. See https://golang.org/pkg/testing/quick/#Generator for more
148157
// information. Generated structs will never contain embedded structs.

Diff for: struct_test.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pack_test
22

33
import (
44
"reflect"
5+
"testing/quick"
56

67
"github.com/renproject/pack"
78
"github.com/renproject/pack/packutil"
@@ -84,9 +85,24 @@ var _ = Describe("Struct", func() {
8485

8586
Context("when getting type information", func() {
8687
It("should return the struct type", func() {
87-
s := pack.NewStruct("foo", pack.NewU64(32))
88-
t := s.Type()
89-
Expect(t.Kind()).To(Equal(pack.KindStruct))
88+
f := func(x pack.Struct) bool {
89+
Expect(x.Type().Kind()).To(Equal(pack.KindStruct))
90+
return true
91+
}
92+
Expect(quick.Check(f, nil)).To(Succeed())
93+
})
94+
})
95+
96+
Context("when stringifying a struct", func() {
97+
It("should equal the JSON representation", func() {
98+
f := func(x pack.Struct) bool {
99+
stringified := x.String()
100+
data, err := x.MarshalJSON()
101+
Expect(err).ToNot(HaveOccurred())
102+
Expect(stringified).To(Equal(string(data)))
103+
return true
104+
}
105+
Expect(quick.Check(f, nil)).To(Succeed())
90106
})
91107
})
92108
})

Diff for: value.go

+9
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ func (typed *Typed) Unmarshal(buf []byte, rem int) ([]byte, int, error) {
129129
return buf, rem, nil
130130
}
131131

132+
// String returns the struct in its JSON representation.
133+
func (typed Typed) String() string {
134+
data, err := typed.MarshalJSON()
135+
if err != nil {
136+
return fmt.Sprintf(`{"error": %v}`, err)
137+
}
138+
return string(data)
139+
}
140+
132141
// Generate a random well-typed struct. This method is implemented for use in
133142
// quick tests. See https://golang.org/pkg/testing/quick/#Generator for more
134143
// information. Generated typed values will never contain embedded structs.

Diff for: value_test.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pack_test
22

33
import (
44
"reflect"
5+
"testing/quick"
56

67
"github.com/renproject/pack"
78
"github.com/renproject/pack/packutil"
@@ -72,9 +73,24 @@ var _ = Describe("Typed", func() {
7273

7374
Context("when getting type information", func() {
7475
It("should return the struct type", func() {
75-
s := pack.NewTyped("foo", pack.NewU64(32))
76-
t := s.Type()
77-
Expect(t.Kind()).To(Equal(pack.KindStruct))
76+
f := func(x pack.Typed) bool {
77+
Expect(x.Type().Kind()).To(Equal(pack.KindStruct))
78+
return true
79+
}
80+
Expect(quick.Check(f, nil)).To(Succeed())
81+
})
82+
})
83+
84+
Context("when stringifying a typed struct", func() {
85+
It("should equal the JSON representation", func() {
86+
f := func(x pack.Typed) bool {
87+
stringified := x.String()
88+
data, err := x.MarshalJSON()
89+
Expect(err).ToNot(HaveOccurred())
90+
Expect(stringified).To(Equal(string(data)))
91+
return true
92+
}
93+
Expect(quick.Check(f, nil)).To(Succeed())
7894
})
7995
})
8096
})

0 commit comments

Comments
 (0)