Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
8dc4abc0
Unverified
Commit
8dc4abc0
authored
Jul 28, 2023
by
Adrian Sutton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cannon: Support gzip compression for JSON files.
parent
e3bc1e5f
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
73 additions
and
0 deletions
+73
-0
.gitignore
cannon/.gitignore
+1
-0
json.go
cannon/cmd/json.go
+19
-0
json_test.go
cannon/cmd/json_test.go
+53
-0
No files found.
cannon/.gitignore
View file @
8dc4abc0
...
...
@@ -9,6 +9,7 @@ example/bin
contracts/out
state.json
*.json
*.json.gz
*.pprof
*.out
bin
cannon/cmd/json.go
View file @
8dc4abc0
package
cmd
import
(
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
)
func
loadJSON
[
X
any
](
inputPath
string
)
(
*
X
,
error
)
{
if
inputPath
==
""
{
return
nil
,
errors
.
New
(
"no path specified"
)
}
var
f
io
.
ReadCloser
f
,
err
:=
os
.
OpenFile
(
inputPath
,
os
.
O_RDONLY
,
0
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to open file %q: %w"
,
inputPath
,
err
)
}
defer
f
.
Close
()
if
isGzip
(
inputPath
)
{
f
,
err
=
gzip
.
NewReader
(
f
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"create gzip reader: %w"
,
err
)
}
defer
f
.
Close
()
}
var
state
X
if
err
:=
json
.
NewDecoder
(
f
)
.
Decode
(
&
state
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to decode file %q: %w"
,
inputPath
,
err
)
...
...
@@ -33,6 +43,11 @@ func writeJSON[X any](outputPath string, value X, outIfEmpty bool) error {
}
defer
f
.
Close
()
out
=
f
if
isGzip
(
outputPath
)
{
g
:=
gzip
.
NewWriter
(
f
)
defer
g
.
Close
()
out
=
g
}
}
else
if
outIfEmpty
{
out
=
os
.
Stdout
}
else
{
...
...
@@ -48,3 +63,7 @@ func writeJSON[X any](outputPath string, value X, outIfEmpty bool) error {
}
return
nil
}
func
isGzip
(
path
string
)
bool
{
return
strings
.
HasSuffix
(
path
,
".gz"
)
}
cannon/cmd/json_test.go
0 → 100644
View file @
8dc4abc0
package
cmd
import
(
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func
TestRoundTripJSON
(
t
*
testing
.
T
)
{
dir
:=
t
.
TempDir
()
file
:=
filepath
.
Join
(
dir
,
"test.json"
)
data
:=
&
jsonTestData
{
A
:
"yay"
,
B
:
3
}
err
:=
writeJSON
(
file
,
data
,
false
)
require
.
NoError
(
t
,
err
)
// Confirm the file is uncompressed
fileContent
,
err
:=
os
.
ReadFile
(
file
)
require
.
NoError
(
t
,
err
)
err
=
json
.
Unmarshal
(
fileContent
,
&
jsonTestData
{})
require
.
NoError
(
t
,
err
)
var
result
*
jsonTestData
result
,
err
=
loadJSON
[
jsonTestData
](
file
)
require
.
NoError
(
t
,
err
)
require
.
EqualValues
(
t
,
data
,
result
)
}
func
TestRoundTripJSONWithGzip
(
t
*
testing
.
T
)
{
dir
:=
t
.
TempDir
()
file
:=
filepath
.
Join
(
dir
,
"test.json.gz"
)
data
:=
&
jsonTestData
{
A
:
"yay"
,
B
:
3
}
err
:=
writeJSON
(
file
,
data
,
false
)
require
.
NoError
(
t
,
err
)
// Confirm the file isn't raw JSON
fileContent
,
err
:=
os
.
ReadFile
(
file
)
require
.
NoError
(
t
,
err
)
err
=
json
.
Unmarshal
(
fileContent
,
&
jsonTestData
{})
require
.
Error
(
t
,
err
,
"should not be able to decode without decompressing"
)
var
result
*
jsonTestData
result
,
err
=
loadJSON
[
jsonTestData
](
file
)
require
.
NoError
(
t
,
err
)
require
.
EqualValues
(
t
,
data
,
result
)
}
type
jsonTestData
struct
{
A
string
`json:"a"`
B
int
`json:"b"`
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment