diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-04 17:50:16 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-04 17:50:16 -0700 |
commit | 07d40ba94387eb2420093566cf11087ba66f391f (patch) | |
tree | b9a3e4cdf56bb555a42c88d3f8e1bb833e8c59b9 /roundtrip_test.go | |
parent | 4fac0e4475dfcfa5e209467e33d58d0e6beddb31 (diff) | |
parent | 183832b22bf2730645aebffab41211fbfec2e18c (diff) |
Merge branch 'lukeshu/fix'
Diffstat (limited to 'roundtrip_test.go')
-rw-r--r-- | roundtrip_test.go | 91 |
1 files changed, 73 insertions, 18 deletions
diff --git a/roundtrip_test.go b/roundtrip_test.go index 71ca6d0..01d6f13 100644 --- a/roundtrip_test.go +++ b/roundtrip_test.go @@ -6,9 +6,9 @@ package lowmemjson_test import ( "bytes" + "encoding/json" "os" "path/filepath" - "strings" "testing" "github.com/stretchr/testify/require" @@ -16,7 +16,7 @@ import ( "git.lukeshu.com/go/lowmemjson" ) -type ScanDevicesResult map[string]ScanOneDeviceResult +type ScanDevicesResult map[uint64]ScanOneDeviceResult type ScanOneDeviceResult struct { FoundExtentCSums []SysExtentCSum @@ -27,27 +27,82 @@ type SysExtentCSum struct { Sums SumRun } +type Optional[T any] struct { + OK bool + Val T +} + +var ( + _ json.Marshaler = Optional[bool]{} + _ json.Unmarshaler = (*Optional[bool])(nil) +) + +func (o Optional[T]) MarshalJSON() ([]byte, error) { + if o.OK { + return json.Marshal(o.Val) + } else { + return []byte("null"), nil + } +} + +func (o *Optional[T]) UnmarshalJSON(dat []byte) error { + if string(dat) == "null" { + *o = Optional[T]{} + return nil + } + o.OK = true + return json.Unmarshal(dat, &o.Val) +} + +type QualifiedPhysicalAddr struct { + Dev uint64 + Addr int64 +} + +type Mapping struct { + LAddr int64 + PAddr QualifiedPhysicalAddr + Size int64 + SizeLocked bool `json:",omitempty"` + Flags Optional[uint64] `json:",omitempty"` +} + func TestRoundTrip(t *testing.T) { t.Parallel() - dents, err := os.ReadDir(filepath.Join("testdata", "roundtrip")) - require.NoError(t, err) - for _, dent := range dents { - filename := dent.Name() - if !strings.HasSuffix(filename, ".json") { - continue - } - t.Run(strings.TrimSuffix(filename, ".json"), func(t *testing.T) { + + type testcase struct { + ObjPtr any + Cfg lowmemjson.ReEncoderConfig + } + testcases := map[string]testcase{ + "scandevices": { + ObjPtr: new(ScanDevicesResult), + Cfg: lowmemjson.ReEncoderConfig{ + Indent: "\t", + ForceTrailingNewlines: true, + CompactIfUnder: 16, + }, + }, + "mappings": { + ObjPtr: new([]Mapping), + Cfg: lowmemjson.ReEncoderConfig{ + Indent: "\t", + ForceTrailingNewlines: true, + CompactIfUnder: 120, + }, + }, + } + + for tcName, tc := range testcases { + tcName := tcName + tc := tc + t.Run(tcName, func(t *testing.T) { t.Parallel() - inBytes, err := os.ReadFile(filepath.Join("testdata", "roundtrip", filename)) + inBytes, err := os.ReadFile(filepath.Join("testdata", "roundtrip", tcName+".json")) // #nosec G304 require.NoError(t, err) - var obj ScanDevicesResult - require.NoError(t, lowmemjson.NewDecoder(bytes.NewReader(inBytes)).DecodeThenEOF(&obj)) + require.NoError(t, lowmemjson.NewDecoder(bytes.NewReader(inBytes)).DecodeThenEOF(tc.ObjPtr)) var outBytes bytes.Buffer - require.NoError(t, lowmemjson.NewEncoder(lowmemjson.NewReEncoder(&outBytes, lowmemjson.ReEncoderConfig{ - Indent: "\t", - ForceTrailingNewlines: true, - CompactIfUnder: 16, //nolint:gomnd // This is what looks nice. - })).Encode(obj)) + require.NoError(t, lowmemjson.NewEncoder(lowmemjson.NewReEncoder(&outBytes, tc.Cfg)).Encode(tc.ObjPtr)) require.Equal(t, string(inBytes), outBytes.String()) }) } |