Skip to content

Commit

Permalink
chore: replace ujson with official json
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <[email protected]>
  • Loading branch information
n0izn0iz committed Apr 11, 2024
1 parent 4dc53b5 commit b35749e
Show file tree
Hide file tree
Showing 48 changed files with 809 additions and 1,814 deletions.
2 changes: 1 addition & 1 deletion examples/gno.land/p/demo/teritori/dao_core/gno.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module gno.land/p/demo/teritori/dao_core

require (
gno.land/p/demo/json v0.0.0-latest
gno.land/p/demo/teritori/dao_interfaces v0.0.0-latest
gno.land/p/demo/teritori/markdown_utils v0.0.0-latest
gno.land/p/demo/teritori/ujson v0.0.0-latest
)
26 changes: 17 additions & 9 deletions examples/gno.land/p/demo/teritori/dao_core/messages.gno
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package core

import (
"gno.land/p/demo/json"
dao_interfaces "gno.land/p/demo/teritori/dao_interfaces"
"gno.land/p/demo/teritori/ujson"
)

// UpdateProposalModules
Expand All @@ -12,6 +12,8 @@ type UpdateProposalModulesExecutableMessage struct {
ToDisable []int
}

var _ dao_interfaces.ExecutableMessage = &UpdateProposalModulesExecutableMessage{}

func (msg UpdateProposalModulesExecutableMessage) Type() string {
return "gno.land/p/demo/teritori/dao_core.UpdateProposalModules"
}
Expand All @@ -20,18 +22,20 @@ func (msg *UpdateProposalModulesExecutableMessage) String() string {
panic(ErrNotImplemented)
}

func (msg *UpdateProposalModulesExecutableMessage) ToJSON() string {
func (msg *UpdateProposalModulesExecutableMessage) ToJSON() *json.Node {
panic(ErrNotImplemented)
}

func (msg *UpdateProposalModulesExecutableMessage) FromJSON(ast *ujson.JSONASTNode) {
func (msg *UpdateProposalModulesExecutableMessage) FromJSON(ast *json.Node) {
panic(ErrNotImplemented)
}

type UpdateProposalModulesMessageHandler struct {
dao dao_interfaces.IDAOCore
}

var _ dao_interfaces.MessageHandler = &UpdateProposalModulesMessageHandler{}

func NewUpdateProposalModulesMessageHandler(dao dao_interfaces.IDAOCore) *UpdateProposalModulesMessageHandler {
return &UpdateProposalModulesMessageHandler{dao: dao}
}
Expand All @@ -45,8 +49,8 @@ func (handler *UpdateProposalModulesMessageHandler) Execute(message dao_interfac
handler.dao.UpdateProposalModules(msg.ToAdd, msg.ToDisable)
}

func (handler *UpdateProposalModulesMessageHandler) MessageFromJSON(ast *ujson.JSONASTNode) dao_interfaces.ExecutableMessage {
panic(ErrNotSupported)
func (handler *UpdateProposalModulesMessageHandler) Instantiate() dao_interfaces.ExecutableMessage {
return &UpdateProposalModulesExecutableMessage{}
}

// UpdateVotingModule
Expand All @@ -55,6 +59,8 @@ type UpdateVotingModuleExecutableMessage struct {
Module dao_interfaces.IVotingModule
}

var _ dao_interfaces.ExecutableMessage = &UpdateVotingModuleExecutableMessage{}

func (msg UpdateVotingModuleExecutableMessage) Type() string {
return "gno.land/p/demo/teritori/dao_core.UpdateVotingModule"
}
Expand All @@ -63,18 +69,20 @@ func (msg *UpdateVotingModuleExecutableMessage) String() string {
panic(ErrNotImplemented)
}

func (msg *UpdateVotingModuleExecutableMessage) ToJSON() string {
func (msg *UpdateVotingModuleExecutableMessage) ToJSON() *json.Node {
panic(ErrNotImplemented)
}

func (msg *UpdateVotingModuleExecutableMessage) FromJSON(ast *ujson.JSONASTNode) {
func (msg *UpdateVotingModuleExecutableMessage) FromJSON(ast *json.Node) {
panic(ErrNotImplemented)
}

type UpdateVotingModuleMessageHandler struct {
dao dao_interfaces.IDAOCore
}

var _ dao_interfaces.MessageHandler = &UpdateVotingModuleMessageHandler{}

func NewUpdateVotingModuleMessageHandler(dao dao_interfaces.IDAOCore) *UpdateVotingModuleMessageHandler {
return &UpdateVotingModuleMessageHandler{dao: dao}
}
Expand All @@ -88,6 +96,6 @@ func (handler *UpdateVotingModuleMessageHandler) Execute(message dao_interfaces.
handler.dao.UpdateVotingModule(msg.Module)
}

func (handler *UpdateVotingModuleMessageHandler) MessageFromJSON(ast *ujson.JSONASTNode) dao_interfaces.ExecutableMessage {
panic(ErrNotSupported)
func (handler *UpdateVotingModuleMessageHandler) Instantiate() dao_interfaces.ExecutableMessage {
return &UpdateVotingModuleExecutableMessage{}
}
2 changes: 1 addition & 1 deletion examples/gno.land/p/demo/teritori/dao_interfaces/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ module gno.land/p/demo/teritori/dao_interfaces

require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/teritori/ujson v0.0.0-latest
gno.land/p/demo/json v0.0.0-latest
)
8 changes: 4 additions & 4 deletions examples/gno.land/p/demo/teritori/dao_interfaces/messages.gno
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package dao_interfaces

import (
"gno.land/p/demo/teritori/ujson"
"gno.land/p/demo/json"
)

type ExecutableMessage interface {
ujson.JSONAble
ujson.FromJSONAble
ToJSON() *json.Node
FromJSON(ast *json.Node)

String() string
Type() string
}

type MessageHandler interface {
Execute(message ExecutableMessage)
MessageFromJSON(ast *ujson.JSONASTNode) ExecutableMessage
Instantiate() ExecutableMessage
Type() string
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dao_interfaces

import (
"gno.land/p/demo/avl"
"gno.land/p/demo/teritori/ujson"
"gno.land/p/demo/json"
)

type MessagesRegistry struct {
Expand All @@ -24,21 +24,18 @@ func (r *MessagesRegistry) Remove(t string) {
r.handlers.Remove(t)
}

func (r *MessagesRegistry) MessagesFromJSON(messagesJSON string) []ExecutableMessage {
slice := ujson.ParseSlice(messagesJSON)
msgs := make([]ExecutableMessage, 0, len(slice))
for _, child := range slice {
var messageType string
var payload *ujson.JSONASTNode
child.ParseObject([]*ujson.ParseKV{
{Key: "type", Value: &messageType},
{Key: "payload", Value: &payload},
})
func (r *MessagesRegistry) MessagesFromJSON(slice []*json.Node) []ExecutableMessage {
msgs := make([]ExecutableMessage, len(slice))
for i, elem := range slice {
messageType := json.Must(elem.GetKey("type")).MustString()
payload := json.Must(elem.GetKey("payload"))
h, ok := r.handlers.Get(messageType)
if !ok {
panic("invalid ExecutableMessage: invalid message type")
}
msgs = append(msgs, h.(MessageHandler).MessageFromJSON(payload))
instance := h.(MessageHandler).Instantiate()
instance.FromJSON(payload)
msgs[i] = instance
}
return msgs
}
Expand All @@ -61,15 +58,17 @@ type RegisterHandlerExecutableMessage struct {
Handler MessageHandler
}

var _ ExecutableMessage = &RegisterHandlerExecutableMessage{}

func (m RegisterHandlerExecutableMessage) Type() string {
return "gno.land/p/demo/teritori/dao_interfaces.RegisterHandler"
}

func (m *RegisterHandlerExecutableMessage) FromJSON(ast *ujson.JSONASTNode) {
func (m *RegisterHandlerExecutableMessage) FromJSON(ast *json.Node) {
panic("not implemented")
}

func (m *RegisterHandlerExecutableMessage) ToJSON() string {
func (m *RegisterHandlerExecutableMessage) ToJSON() *json.Node {
panic("not implemented")
}

Expand All @@ -81,6 +80,8 @@ type RegisterHandlerExecutableMessageHandler struct {
registry *MessagesRegistry
}

var _ MessageHandler = &RegisterHandlerExecutableMessageHandler{}

func NewRegisterHandlerExecutableMessageHandler(registry *MessagesRegistry) *RegisterHandlerExecutableMessageHandler {
return &RegisterHandlerExecutableMessageHandler{registry: registry}
}
Expand All @@ -89,8 +90,8 @@ func (h RegisterHandlerExecutableMessageHandler) Type() string {
return RegisterHandlerExecutableMessage{}.Type()
}

func (h *RegisterHandlerExecutableMessageHandler) MessageFromJSON(ast *ujson.JSONASTNode) ExecutableMessage {
panic("not implemented")
func (h *RegisterHandlerExecutableMessageHandler) Instantiate() ExecutableMessage {
return &RegisterHandlerExecutableMessage{}
}

func (h *RegisterHandlerExecutableMessageHandler) Execute(msg ExecutableMessage) {
Expand All @@ -101,16 +102,18 @@ type RemoveHandlerExecutableMessage struct {
HandlerType string
}

var _ ExecutableMessage = &RemoveHandlerExecutableMessage{}

func (m RemoveHandlerExecutableMessage) Type() string {
return "gno.land/p/demo/teritori/dao_interfaces.RemoveHandler"
}

func (m *RemoveHandlerExecutableMessage) FromJSON(ast *ujson.JSONASTNode) {
ast.ParseAny(&m.HandlerType)
func (m *RemoveHandlerExecutableMessage) FromJSON(ast *json.Node) {
m.HandlerType = ast.MustString()
}

func (m *RemoveHandlerExecutableMessage) ToJSON() string {
return ujson.FormatAny(m.HandlerType)
func (m *RemoveHandlerExecutableMessage) ToJSON() *json.Node {
return json.StringNode("", m.HandlerType)
}

func (m *RemoveHandlerExecutableMessage) String() string {
Expand All @@ -121,6 +124,8 @@ type RemoveHandlerExecutableMessageHandler struct {
registry *MessagesRegistry
}

var _ MessageHandler = &RemoveHandlerExecutableMessageHandler{}

func NewRemoveHandlerExecutableMessageHandler(registry *MessagesRegistry) *RemoveHandlerExecutableMessageHandler {
return &RemoveHandlerExecutableMessageHandler{registry: registry}
}
Expand All @@ -129,10 +134,8 @@ func (h RemoveHandlerExecutableMessageHandler) Type() string {
return RemoveHandlerExecutableMessage{}.Type()
}

func (h *RemoveHandlerExecutableMessageHandler) MessageFromJSON(ast *ujson.JSONASTNode) ExecutableMessage {
msg := &RemoveHandlerExecutableMessage{}
ast.ParseAny(msg)
return msg
func (h *RemoveHandlerExecutableMessageHandler) Instantiate() ExecutableMessage {
return &RemoveHandlerExecutableMessage{}
}

func (h *RemoveHandlerExecutableMessageHandler) Execute(msg ExecutableMessage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dao_interfaces

import (
"testing"

"gno.land/p/demo/json"
)

func TestRegistry(t *testing.T) {
Expand All @@ -15,7 +17,7 @@ func TestRegistry(t *testing.T) {
registry.Execute(registerMsg)

// Test messages execution
msgs := registry.MessagesFromJSON(`[{"type":"CopyMessage","payload":"Hello"}]`)
msgs := registry.MessagesFromJSON(json.Must(json.Unmarshal(`[{"type":"CopyMessage","payload":"Hello"}]`)).MustArray())
if len(msgs) != 1 {
t.Errorf("Expected 1 message, got %d", len(msgs))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dao_interfaces

import (
"gno.land/p/demo/teritori/ujson"
"gno.land/p/demo/json"
)

type CopyMessage struct {
Expand All @@ -16,12 +16,12 @@ func (m *CopyMessage) String() string {
return m.Value
}

func (m *CopyMessage) FromJSON(ast *ujson.JSONASTNode) {
ast.ParseAny(&m.Value)
func (m *CopyMessage) FromJSON(ast *json.Node) {
m.Value = ast.MustString()
}

func (m *CopyMessage) ToJSON() string {
return ujson.FormatString(m.Value)
func (m *CopyMessage) ToJSON() *json.Node {
return json.StringNode("", m.Value)
}

type CopyMessageHandler struct {
Expand All @@ -47,8 +47,6 @@ func (h CopyMessageHandler) Type() string {
return "CopyMessage"
}

func (h *CopyMessageHandler) MessageFromJSON(ast *ujson.JSONASTNode) ExecutableMessage {
var msg CopyMessage
ast.ParseAny(&msg)
return &msg
func (h *CopyMessageHandler) Instantiate() ExecutableMessage {
return &CopyMessage{}
}
Loading

0 comments on commit b35749e

Please sign in to comment.