-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
conduit: Reorganize ahead of repo split. (#1489)
- Loading branch information
Showing
40 changed files
with
290 additions
and
289 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package filewriter | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/algorand/go-codec/codec" | ||
|
||
"github.com/algorand/go-algorand-sdk/v2/encoding/json" | ||
) | ||
|
||
var prettyHandle *codec.JsonHandle | ||
var jsonStrictHandle *codec.JsonHandle | ||
|
||
func init() { | ||
prettyHandle = new(codec.JsonHandle) | ||
prettyHandle.ErrorIfNoField = json.CodecHandle.ErrorIfNoField | ||
prettyHandle.ErrorIfNoArrayExpand = json.CodecHandle.ErrorIfNoArrayExpand | ||
prettyHandle.Canonical = json.CodecHandle.Canonical | ||
prettyHandle.RecursiveEmptyCheck = json.CodecHandle.RecursiveEmptyCheck | ||
prettyHandle.Indent = json.CodecHandle.Indent | ||
prettyHandle.HTMLCharsAsIs = json.CodecHandle.HTMLCharsAsIs | ||
prettyHandle.MapKeyAsString = true | ||
prettyHandle.Indent = 2 | ||
|
||
jsonStrictHandle = new(codec.JsonHandle) | ||
jsonStrictHandle.ErrorIfNoField = prettyHandle.ErrorIfNoField | ||
jsonStrictHandle.ErrorIfNoArrayExpand = prettyHandle.ErrorIfNoArrayExpand | ||
jsonStrictHandle.Canonical = prettyHandle.Canonical | ||
jsonStrictHandle.RecursiveEmptyCheck = prettyHandle.RecursiveEmptyCheck | ||
jsonStrictHandle.Indent = prettyHandle.Indent | ||
jsonStrictHandle.HTMLCharsAsIs = prettyHandle.HTMLCharsAsIs | ||
jsonStrictHandle.MapKeyAsString = true | ||
} | ||
|
||
// EncodeJSONToFile is used to encode an object to a file. If the file ends in .gz it will be gzipped. | ||
func EncodeJSONToFile(filename string, v interface{}, pretty bool) error { | ||
var writer io.Writer | ||
|
||
file, err := os.Create(filename) | ||
if err != nil { | ||
return fmt.Errorf("EncodeJSONToFile(): failed to create %s: %w", filename, err) | ||
} | ||
defer file.Close() | ||
|
||
if strings.HasSuffix(filename, ".gz") { | ||
gz := gzip.NewWriter(file) | ||
gz.Name = filename | ||
defer gz.Close() | ||
writer = gz | ||
} else { | ||
writer = file | ||
} | ||
|
||
var handle *codec.JsonHandle | ||
if pretty { | ||
handle = prettyHandle | ||
} else { | ||
handle = jsonStrictHandle | ||
} | ||
enc := codec.NewEncoder(writer, handle) | ||
return enc.Encode(v) | ||
} | ||
|
||
// DecodeJSONFromFile is used to decode a file to an object. | ||
func DecodeJSONFromFile(filename string, v interface{}, strict bool) error { | ||
// Streaming into the decoder was slow. | ||
fileBytes, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return fmt.Errorf("DecodeJSONFromFile(): failed to read %s: %w", filename, err) | ||
} | ||
|
||
var reader io.Reader = bytes.NewReader(fileBytes) | ||
|
||
if strings.HasSuffix(filename, ".gz") { | ||
gz, err := gzip.NewReader(reader) | ||
defer gz.Close() | ||
if err != nil { | ||
return fmt.Errorf("DecodeJSONFromFile(): failed to make gzip reader: %w", err) | ||
} | ||
reader = gz | ||
} | ||
var handle *codec.JsonHandle | ||
if strict { | ||
handle = json.CodecHandle | ||
} else { | ||
handle = json.LenientCodecHandle | ||
} | ||
|
||
enc := codec.NewDecoder(reader, handle) | ||
return enc.Decode(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package filewriter | ||
|
||
import ( | ||
"io/ioutil" | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEncodeToAndFromFile(t *testing.T) { | ||
tempdir := t.TempDir() | ||
|
||
type test struct { | ||
One string `json:"one"` | ||
Two int `json:"two"` | ||
Strict map[int]string `json:"strict"` | ||
} | ||
data := test{ | ||
One: "one", | ||
Two: 2, | ||
Strict: map[int]string{ | ||
0: "int-key", | ||
}, | ||
} | ||
|
||
{ | ||
pretty := path.Join(tempdir, "pretty.json") | ||
err := EncodeJSONToFile(pretty, data, true) | ||
require.NoError(t, err) | ||
require.FileExists(t, pretty) | ||
var testDecode test | ||
err = DecodeJSONFromFile(pretty, &testDecode, false) | ||
require.Equal(t, data, testDecode) | ||
|
||
// Check the pretty printing | ||
bytes, err := ioutil.ReadFile(pretty) | ||
require.NoError(t, err) | ||
require.Contains(t, string(bytes), " \"one\": \"one\",\n") | ||
require.Contains(t, string(bytes), `"0": "int-key"`) | ||
} | ||
|
||
{ | ||
small := path.Join(tempdir, "small.json") | ||
err := EncodeJSONToFile(small, data, false) | ||
require.NoError(t, err) | ||
require.FileExists(t, small) | ||
var testDecode test | ||
err = DecodeJSONFromFile(small, &testDecode, false) | ||
require.Equal(t, data, testDecode) | ||
} | ||
|
||
// gzip test | ||
{ | ||
small := path.Join(tempdir, "small.json.gz") | ||
err := EncodeJSONToFile(small, data, false) | ||
require.NoError(t, err) | ||
require.FileExists(t, small) | ||
var testDecode test | ||
err = DecodeJSONFromFile(small, &testDecode, false) | ||
require.Equal(t, data, testDecode) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.