Skip to content

Commit

Permalink
fix: WakuMessage json encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Nov 8, 2023
1 parent 150ade6 commit 9012aa0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
];
doCheck = false;
# FIXME: This needs to be manually changed when updating modules.
vendorSha256 = "sha256-4xChSKAkwwrFp5/ZMnhtvsR4drVfw1cLE3YXwVHeW0A=";
vendorSha256 = "sha256-1m5byn1CVD8Awzbo9XY94FgitC+uetTfon/8bBh5DiE=";
# Fix for 'nix run' trying to execute 'go-waku'.
meta = { mainProgram = "waku"; };
};
Expand Down
29 changes: 29 additions & 0 deletions waku/v2/protocol/pb/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package pb

import (
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

func (m *WakuMessage) MarshalJSON() ([]byte, error) {
return (protojson.MarshalOptions{}).Marshal(m)
}

func Unmarshal(data []byte) (*WakuMessage, error) {
msg := &WakuMessage{}
err := proto.Unmarshal(data, msg)
if err != nil {
return nil, err
}

err = msg.Validate()
if err != nil {
return nil, err
}

return msg, nil
}

func (m *WakuMessage) UnmarshalJSON(data []byte) error {
return (protojson.UnmarshalOptions{}).Unmarshal(data, m)
}
18 changes: 0 additions & 18 deletions waku/v2/protocol/pb/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package pb

import (
"errors"

"google.golang.org/protobuf/proto"
)

const MaxMetaAttrLength = 64
Expand All @@ -29,19 +27,3 @@ func (msg *WakuMessage) Validate() error {

return nil
}

func Unmarshal(data []byte) (*WakuMessage, error) {
msg := &WakuMessage{}
err := proto.Unmarshal(data, msg)
if err != nil {
return nil, err
}

err = msg.Validate()
if err != nil {
return nil, err
}

return msg, nil

}

0 comments on commit 9012aa0

Please sign in to comment.