Skip to content

Commit

Permalink
chore: switch jsonutil -> ujson
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <[email protected]>
  • Loading branch information
n0izn0iz committed Aug 25, 2023
1 parent eb79f91 commit 7964e32
Show file tree
Hide file tree
Showing 22 changed files with 67 additions and 1,198 deletions.
16 changes: 8 additions & 8 deletions examples/gno.land/p/demo/daodao/interfaces/dao_interfaces.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strconv"

"gno.land/p/demo/avl"
"gno.land/p/demo/jsonutil"
"gno.land/p/demo/ujson"
)

type IVotingModule interface {
Expand All @@ -21,7 +21,7 @@ type Ballot struct {
}

func (b Ballot) ToJSON() string {
return jsonutil.FormatObject([]jsonutil.KeyValue{
return ujson.FormatObject([]ujson.FormatKV{
{Key: "power", Value: b.Power},
{Key: "vote", Value: b.Vote},
{Key: "rationale", Value: b.Rationale},
Expand Down Expand Up @@ -65,7 +65,7 @@ func (v *Votes) Total() uint64 {
}

func (v Votes) ToJSON() string {
return jsonutil.FormatObject([]jsonutil.KeyValue{
return ujson.FormatObject([]ujson.FormatKV{
{Key: "yes", Value: v.Yes},
{Key: "no", Value: v.No},
{Key: "abstain", Value: v.Abstain},
Expand All @@ -84,15 +84,15 @@ type Proposal struct {
Status ProposalStatus
}

var _ jsonutil.JSONAble = (*Proposal)(nil)
var _ ujson.JSONAble = (*Proposal)(nil)

func (p Proposal) ToJSON() string {
return jsonutil.FormatObject([]jsonutil.KeyValue{
return ujson.FormatObject([]ujson.FormatKV{
{Key: "id", Value: p.ID},
{Key: "title", Value: p.Title},
{Key: "description", Value: p.Description},
{Key: "proposer", Value: p.Proposer},
{Key: "messages", Value: jsonutil.FormatSlice(p.Messages), Raw: true},
{Key: "messages", Value: ujson.FormatSlice(p.Messages), Raw: true},
{Key: "ballots", Value: p.Ballots},
{Key: "votes", Value: p.Votes},
{Key: "status", Value: p.Status},
Expand All @@ -108,7 +108,7 @@ const (
)

func (p ProposalStatus) ToJSON() string {
return jsonutil.FormatString(p.String())
return ujson.FormatString(p.String())
}

func (p ProposalStatus) String() string {
Expand All @@ -133,7 +133,7 @@ const (
)

func (v Vote) ToJSON() string {
return jsonutil.FormatString(v.String())
return ujson.FormatString(v.String())
}

func (v Vote) String() string {
Expand Down
48 changes: 8 additions & 40 deletions examples/gno.land/p/demo/daodao/interfaces/dao_messages.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"strings"

"gno.land/p/demo/avl"
"gno.land/p/demo/jsonutil"
"gno.land/p/demo/ujson"
)

type MessageHandler interface {
Execute(message ExecutableMessage)
FromBinary(b []byte) ExecutableMessage
FromJSON(ast *jsonutil.JSONASTNode) ExecutableMessage
MessageFromJSON(ast *ujson.JSONASTNode) ExecutableMessage
Type() string
}

Expand All @@ -28,56 +27,25 @@ func (r *MessagesRegistry) Register(handler MessageHandler) {
r.handlers.Set(handler.Type(), handler)
}

func (r *MessagesRegistry) FromBinary(b []byte) ExecutableMessage {
if len(b) < 2 {
panic("invalid ExecutableMessage: invalid length")
}
l := binary.BigEndian.Uint16(b[:2])
if len(b) < int(l+2) {
panic("invalid ExecutableMessage: invalid length")
}
t := string(b[2 : l+2])

h, ok := r.handlers.Get(t)
if !ok {
panic("invalid ExecutableMessage: invalid message type")
}
return h.(MessageHandler).FromBinary(b)
}

func (r *MessagesRegistry) FromJSON(s string) []ExecutableMessage {
ast := jsonutil.TokenizeAndParse(s)
if ast.Kind != jsonutil.JSONKindArray {
panic("msgs is not an array")
}
msgs := make([]ExecutableMessage, 0, len(ast.ArrayChildren))
func (r *MessagesRegistry) MessagesFromJSON(ast *ujson.JSONASTNode) []ExecutableMessage {
slice := ast.ParseSlice()
msgs := make([]ExecutableMessage, 0, len(slice))
for _, child := range ast.ArrayChildren {
if child.Kind != jsonutil.JSONKindObject {
panic("invalid ExecutableMessage: message container is not an object")
}
var messageType string
var payload *jsonutil.JSONASTNode
jsonutil.ParseObjectAST(child, []*jsonutil.ParseKV{
var payload *ujson.JSONASTNode
child.ParseObject([]*ujson.ParseKV{
{Key: "type", Value: &messageType},
{Key: "payload", Value: &payload},
})
h, ok := r.handlers.Get(messageType)
if !ok {
panic("invalid ExecutableMessage: invalid message type")
}
msgs = append(msgs, h.(MessageHandler).FromJSON(payload))
msgs = append(msgs, h.(MessageHandler).MessageFromJSON(payload))
}
return msgs
}

func (r *MessagesRegistry) FromBase64String(s string) ExecutableMessage {
b, err := base64.RawURLEncoding.DecodeString(s)
if err != nil {
panic("invalid ExecutableMessage: invalid base64 string")
}
return r.FromBinary(b)
}

func (r *MessagesRegistry) Execute(msg ExecutableMessage) {
h, ok := r.handlers.Get(msg.Type())
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/p/demo/daodao/interfaces/gno.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module gno.land/p/demo/daodao/interfaces

require (
"gno.land/p/demo/jsonutil" v0.0.0-latest
"gno.land/p/demo/ujson" v0.0.0-latest
"gno.land/p/demo/avl" v0.0.0-latest
)
6 changes: 3 additions & 3 deletions examples/gno.land/p/demo/daodao/interfaces/proposal_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"gno.land/p/demo/avl"
"gno.land/p/demo/jsonutil"
"gno.land/p/demo/ujson"
)

type NoopMessage struct{}
Expand All @@ -24,7 +24,7 @@ func (m NoopMessage) Type() string {
}

func (m NoopMessage) ToJSON() string {
return jsonutil.FormatString(m.String())
return ujson.FormatString(m.String())
}

func TestProposalJSON(t *testing.T) {
Expand All @@ -51,7 +51,7 @@ func TestProposalJSON(t *testing.T) {
},
}
props[0].Ballots.Set("0x1234567890", Ballot{Power: 1, Vote: VoteYes, Rationale: "test"})
str := jsonutil.FormatSlice(props)
str := ujson.FormatSlice(props)
expected := `[{"id":0,"title":"Prop #0","description":"Wolol0\n\t\r","proposer":"0x1234567890","messages":[],"ballots":{"0x1234567890":{"power":1,"vote":"Yes","rationale":"test"}},"votes":{"yes":7,"no":21,"abstain":42},"status":"Open"},{"id":1,"title":"Prop #1","description":"Wolol1\\\"","proposer":"0x1234567890","messages":["noop","noop","noop"],"ballots":{},"votes":{"yes":0,"no":0,"abstain":0},"status":"Executed"}]`
if expected != str {
t.Fatalf("JSON does not match, expected %s, got %s", expected, str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"gno.land/p/demo/avl"
dao_core "gno.land/p/demo/daodao/core"
dao_interfaces "gno.land/p/demo/daodao/interfaces"
"gno.land/p/demo/jsonutil"
"gno.land/p/demo/ujson"
)

type DAOProposalSingleOpts struct {
Expand Down Expand Up @@ -261,7 +261,7 @@ func (d *daoProposalSingle) Proposals() []dao_interfaces.Proposal {
}

func (d *daoProposalSingle) ProposalsJSON() string {
return jsonutil.FormatSlice(d.proposals)
return ujson.FormatSlice(d.proposals)
}

func (d *daoProposalSingle) Threshold() dao_interfaces.Threshold {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package grc4
package dao_voting_group

import (
"std"
"testing"

"gno.land/r/demo/groups"
)

func Test(t *testing.T) {
{
admin := "g14u5eaheavy0ux4dmpykg2gvxpvqvexm9cyg58a"
g := grc4.NewGRC4Group(std.Address(admin), nil)
g := groups.CreateGroup("test_voting_group")
v := NewGRC4Voting(g)
got := v.TotalPower()
expected := uint64(0)
Expand Down
5 changes: 0 additions & 5 deletions examples/gno.land/p/demo/jsonutil/gno.mod

This file was deleted.

118 changes: 0 additions & 118 deletions examples/gno.land/p/demo/jsonutil/json_test.gno

This file was deleted.

Loading

0 comments on commit 7964e32

Please sign in to comment.