Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tlb.Ref[T] MarshalJSON + UnmarshalJSON #160

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Go Version
uses: actions/setup-go@v3
with:
go-version: '1.19'
go-version: '1.20'
- name: test
env:
LD_LIBRARY_PATH: ${{ github.workspace }}/libs
Expand Down
38 changes: 29 additions & 9 deletions tlb/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,33 +193,53 @@ func (m *EitherRef[_]) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
return decoder.Unmarshal(c, &m.Value)
}

func (m Ref[_]) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
r := boc.NewCell()
err := Marshal(r, m.Value)
func (r Ref[_]) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
ref := boc.NewCell()
err := Marshal(ref, r.Value)
if err != nil {
return err
}
err = c.AddRef(r)
err = c.AddRef(ref)
return err
}

func (m *Ref[T]) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
r, err := c.NextRef()
func (r *Ref[T]) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
ref, err := c.NextRef()
if err != nil {
return err
}
if r.CellType() == boc.PrunedBranchCell {
if ref.CellType() == boc.PrunedBranchCell {
var value T
m.Value = value
r.Value = value
return nil
}
err = decoder.Unmarshal(r, &m.Value)
err = decoder.Unmarshal(ref, &r.Value)
if err != nil {
return err
}
return nil
}

func (r Ref[T]) MarshalJSON() ([]byte, error) {
return json.Marshal(r.Value)
}

func (r *Ref[T]) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &r.Value); err != nil {
// fallback to decoding Ref as {"Value":...}
// TODO: remove this one day
type v struct {
Value T
}
var value v
if err := json.Unmarshal(b, &value); err != nil {
return err
}
r.Value = value.Value
}
return nil
}

func (n Unary) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
return c.WriteUnary(uint(n))
}
Expand Down
53 changes: 53 additions & 0 deletions tlb/primitives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,56 @@ func TestAny_JSON(t *testing.T) {
t.Fatalf("want isRight: %v, got isRight: %v", data.Data.IsRight, unmarshalled.Data.IsRight)
}
}

func TestRef_MarshalJSON(t *testing.T) {
cell := boc.NewCell()
if err := cell.WriteBytes([]byte("hello")); err != nil {
t.Fatalf("cell.WriteBytes() failed: %v", err)
}
ref := Ref[boc.Cell]{
Value: *cell,
}
bs, err := json.Marshal(ref)
if err != nil {
t.Fatalf("json.Marshal() failed: %v", err)
}
expected := `"b5ee9c7201010101000700000a68656c6c6f"`
if string(bs) != expected {
t.Fatalf("expected: %v, got: %v", expected, string(bs))
}
}

func TestRef_UnmarshalJSON(t *testing.T) {
type testCase struct {
name string
str string
wantErr bool
}
tests := []testCase{
{
str: `{"Value":"b5ee9c7201010101000700000a68656c6c6f"}`,
},
{
str: `"b5ee9c7201010101000700000a68656c6c6f"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ref := Ref[boc.Cell]{}
err := ref.UnmarshalJSON([]byte(tt.str))
if tt.wantErr && err == nil {
t.Errorf("wanted error, but UnmarshalJSON was success")
}
if err != nil {
t.Errorf("UnmarshalJSON() failed: %v", err)
}
s, err := ref.Value.ToBocString()
if err != nil {
t.Errorf("ToBocString() failed: %v", err)
}
if s != "b5ee9c7201010101000700000a68656c6c6f" {
t.Errorf("UnmarshalJSON() failed: %v", err)
}
})
}
}