-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add local mode and nonlocal field tag
Some of the fields may only be needed for over-the-network transfer, but can be skipped when encoding the object locally. For instance, if the object is stored in a blob field in the database, but some parts of it also are stored in other columns in the same row, we can save some space by omitting these fields in the blobs. This is achieved by adding `nonlocal` field tag, `WithEncodeLocal()` encoder option and `WithDecodeLocal()` decoder option. The encoder and the decoder skip fields with `nonlocal` tag when they're in the local mode.
- Loading branch information
Showing
11 changed files
with
269 additions
and
23 deletions.
There are no files selected for viewing
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,8 @@ | ||
package examples | ||
|
||
//go:generate scalegen | ||
|
||
type StructWithNonLocalField struct { | ||
Name string `scale:"max=20"` | ||
SomeID string `scale:"nonlocal, max=20"` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package examples | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/spacemeshos/go-scale" | ||
) | ||
|
||
func TestNonLocal(t *testing.T) { | ||
s := StructWithNonLocalField{ | ||
Name: "foo", | ||
SomeID: "bar", | ||
} | ||
|
||
buf := bytes.NewBuffer(nil) | ||
encoder := scale.NewEncoder(buf) | ||
n, err := s.EncodeScale(encoder) | ||
require.NoError(t, err) | ||
require.Equal(t, 8, n) | ||
|
||
decoder := scale.NewDecoder(bytes.NewReader(buf.Bytes())) | ||
var s1 StructWithNonLocalField | ||
n, err = s1.DecodeScale(decoder) | ||
require.NoError(t, err) | ||
require.Equal(t, 8, n) | ||
require.Equal(t, s, s1) | ||
|
||
buf = bytes.NewBuffer(nil) | ||
encoder = scale.NewEncoder(buf, scale.WithEncodeLocal()) | ||
n, err = s.EncodeScale(encoder) | ||
require.NoError(t, err) | ||
require.Equal(t, 4, n) | ||
|
||
decoder = scale.NewDecoder(bytes.NewReader(buf.Bytes()), scale.WithDecodeLocal()) | ||
var s2 StructWithNonLocalField | ||
n, err = s2.DecodeScale(decoder) | ||
require.NoError(t, err) | ||
require.Equal(t, 4, n) | ||
require.Equal(t, StructWithNonLocalField{Name: "foo"}, s2) | ||
} |
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.