Skip to content

Commit a30233c

Browse files
committed
feat(jsonutils): add LoadAs and MarshalAs functions
This change adds the following functions to the jsonutils package. Unit tests are also provided. `LoadAs[T any](path string) result.Result[T]` reads the content from the given path and ummarshals it into a result of a new instance of T. `UnmarshalAs[T any](content []byte) result.Result[T]` unmarshals the content from the given byte slice into a result of a new instance of T. Signed-off-by: m-d-key <[email protected]>
1 parent 2e0af34 commit a30233c

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

pkg/jsonutils/jsonutils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"fmt"
99
"io"
10+
"os"
1011
"strings"
1112

1213
"github.com/sassoftware/sas-ggdk/pkg/result"
@@ -53,3 +54,17 @@ func UnmarshalFromReader(reader io.Reader, instance any) error {
5354
}
5455
return nil
5556
}
57+
58+
// LoadAs reads the content from the given path and ummarshals it into a result
59+
// of a new instance of T.
60+
func LoadAs[T any](path string) result.Result[T] {
61+
content := result.New(os.ReadFile(path))
62+
return result.FlatMap(UnmarshalAs[T], content)
63+
}
64+
65+
// UnmarshalAs ummarshals the given content into a result of anew instance of T.
66+
func UnmarshalAs[T any](content []byte) result.Result[T] {
67+
var t T
68+
err := json.Unmarshal(content, &t)
69+
return result.New(t, err)
70+
}

pkg/jsonutils/jsonutils_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,60 @@ func Test_UnmarshalFromReader_failingUnmarshal(t *testing.T) {
108108
require.Error(t, err)
109109
}
110110

111+
func Test_LoadAs(t *testing.T) {
112+
expected := person{
113+
Name: "John Smith",
114+
Age: 75,
115+
}
116+
actual := jsonutils.LoadAs[person](toTestdataFilename(jsonFilename))
117+
require.False(t, actual.IsError())
118+
require.Equal(t, expected, actual.MustGet())
119+
}
120+
121+
func Test_LoadAs_ptr(t *testing.T) {
122+
expectedPtr := &person{
123+
Name: "John Smith",
124+
Age: 75,
125+
}
126+
actualPtr := jsonutils.LoadAs[*person](toTestdataFilename(jsonFilename))
127+
require.False(t, actualPtr.IsError())
128+
require.Equal(t, expectedPtr, actualPtr.MustGet())
129+
}
130+
131+
func Test_LoadAs_fail(t *testing.T) {
132+
actual := jsonutils.LoadAs[person](toTestdataFilename("missing.json"))
133+
require.True(t, actual.IsError())
134+
}
135+
136+
func Test_UnmarshalAs(t *testing.T) {
137+
expectedPtr := person{
138+
Name: "John Smith",
139+
Age: 75,
140+
}
141+
content, err := readTestdata(jsonFilename)
142+
require.NoError(t, err)
143+
actualPtr := jsonutils.UnmarshalAs[person](content)
144+
require.False(t, actualPtr.IsError())
145+
require.Equal(t, expectedPtr, actualPtr.MustGet())
146+
}
147+
148+
func Test_UnmarshalAs_ptr(t *testing.T) {
149+
expectedPtr := &person{
150+
Name: "John Smith",
151+
Age: 75,
152+
}
153+
content, err := readTestdata(jsonFilename)
154+
require.NoError(t, err)
155+
actualPtr := jsonutils.UnmarshalAs[*person](content)
156+
require.False(t, actualPtr.IsError())
157+
require.Equal(t, expectedPtr, actualPtr.MustGet())
158+
}
159+
160+
func Test_UnmarshalAs_fail(t *testing.T) {
161+
actual := jsonutils.UnmarshalAs[person]([]byte(`{"invalid": "json`))
162+
require.True(t, actual.IsError())
163+
}
164+
111165
func readTestdata(elements ...string) ([]byte, error) {
112166
filename := toTestdataFilename(elements...)
113167
path := filepath.Clean(filename)

0 commit comments

Comments
 (0)