Skip to content

Commit e49f8fb

Browse files
authored
Merge pull request #17 from runreveal/df/marshal-loader-types
Adds a JSON marshalling handler for loader types:
2 parents 98df6af + 7b0b658 commit e49f8fb

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

loader/loader.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ func (b *Loader[T]) UnmarshalJSON(raw []byte) error {
112112
return json.Unmarshal(raw, b.Builder)
113113
}
114114

115+
func (l Loader[T]) MarshalJSON() ([]byte, error) {
116+
return json.Marshal(l.Builder)
117+
}
118+
115119
func (l Loader[T]) Configure() (T, error) {
116120
var t T
117121
if l.Builder == nil {

loader/loader_test.go

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package loader_test
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"os"
67
"testing"
@@ -40,10 +41,10 @@ func TestLoadConfigUnreg(t *testing.T) {
4041

4142
func TestLoadConfig(t *testing.T) {
4243

43-
loader.Register("aTypeOfSource", func() loader.Builder[Source] { return &srcConfigA{} })
44-
loader.Register("sourceThatCanB", func() loader.Builder[Source] { return &srcConfigB{} })
45-
loader.Register("aTypeOfDest", func() loader.Builder[Destination] { return &dstConfigA{} })
46-
loader.Register("bAllThatUCanB", func() loader.Builder[Destination] { return &dstConfigB{} })
44+
loader.Register("aTypeOfSource", func() loader.Builder[Source] { return &srcConfigA{Type: "aTypeOfSource"} })
45+
loader.Register("sourceThatCanB", func() loader.Builder[Source] { return &srcConfigB{Type: "sourceThatCanB"} })
46+
loader.Register("aTypeOfDest", func() loader.Builder[Destination] { return &dstConfigA{Type: "aTypeOfDest"} })
47+
loader.Register("bAllThatUCanB", func() loader.Builder[Destination] { return &dstConfigB{Type: "bAllThatUCanB"} })
4748

4849
tests := []struct {
4950
name string
@@ -81,12 +82,12 @@ func TestLoadConfig(t *testing.T) {
8182
expected: Config{
8283
Name: "jimmy",
8384
Sources: []loader.Loader[Source]{
84-
{&srcConfigA{"localhost"}},
85-
{&srcConfigB{"gym"}},
85+
{&srcConfigA{Type: "aTypeOfSource", Host: "localhost"}},
86+
{&srcConfigB{Type: "sourceThatCanB", Topic: "gym"}},
8687
},
8788
Destinations: []loader.Loader[Destination]{
88-
{&dstConfigA{"localhost"}},
89-
{&dstConfigB{"output"}},
89+
{&dstConfigA{Type: "aTypeOfDest", Host: "localhost"}},
90+
{&dstConfigB{Type: "bAllThatUCanB", Topic: "output"}},
9091
},
9192
},
9293
err: false,
@@ -151,6 +152,7 @@ type srcB struct{ topic string }
151152
func (s *srcB) Recv() (string, error) { return s.topic, nil }
152153

153154
type srcConfigA struct {
155+
Type string `json:"type"`
154156
Host string `json:"host"`
155157
}
156158

@@ -159,6 +161,7 @@ func (c *srcConfigA) Configure() (Source, error) {
159161
}
160162

161163
type srcConfigB struct {
164+
Type string `json:"type"`
162165
Topic string `json:"topic"`
163166
}
164167

@@ -175,17 +178,57 @@ type dstB struct{ topic string }
175178
func (s *dstB) Send(string) error { return nil }
176179

177180
type dstConfigA struct {
178-
Host string
181+
Type string `json:"type"`
182+
Host string `json:"host"`
179183
}
180184

181185
func (c *dstConfigA) Configure() (Destination, error) {
182186
return &dstA{c.Host}, nil
183187
}
184188

185189
type dstConfigB struct {
186-
Topic string
190+
Type string `json:"type"`
191+
Topic string `json:"topic"`
187192
}
188193

189194
func (c *dstConfigB) Configure() (Destination, error) {
190195
return &dstB{c.Topic}, nil
191196
}
197+
198+
func TestMarshalJSON(t *testing.T) {
199+
// Register test types if not already registered from other tests
200+
loader.Register("testSource", func() loader.Builder[Source] { return &srcConfigA{Type: "testSource"} })
201+
202+
tests := []struct {
203+
name string
204+
loader loader.Loader[Source]
205+
expected string
206+
}{
207+
{
208+
name: "marshal source config",
209+
loader: loader.Loader[Source]{&srcConfigA{Type: "testSource", Host: "test-host"}},
210+
expected: `{"type":"testSource","host":"test-host"}`,
211+
},
212+
}
213+
214+
for _, test := range tests {
215+
t.Run(test.name, func(t *testing.T) {
216+
// Marshal the loader
217+
data, err := json.Marshal(test.loader)
218+
assert.NoError(t, err)
219+
220+
// Compare with expected JSON string
221+
assert.JSONEq(t, test.expected, string(data))
222+
223+
// Try unmarshaling back to verify round-trip
224+
var newLoader loader.Loader[Source]
225+
err = json.Unmarshal(data, &newLoader)
226+
assert.NoError(t, err)
227+
228+
// Verify the unmarshaled object produces the same JSON
229+
newData, err := json.Marshal(newLoader)
230+
assert.NoError(t, err)
231+
assert.JSONEq(t, test.expected, string(newData))
232+
})
233+
}
234+
}

0 commit comments

Comments
 (0)