fix: WakuMessage json encoding (#880)

This commit is contained in:
richΛrd 2023-11-08 12:09:10 -04:00 committed by GitHub
parent 70cb6de576
commit 3d217ed5ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 19 deletions

View File

@ -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"; };
};

View File

@ -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)
}

View File

@ -2,8 +2,6 @@ package pb
import (
"errors"
"google.golang.org/protobuf/proto"
)
const MaxMetaAttrLength = 64
@ -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
}