Skip to content

Commit

Permalink
feat: added NewID function for jsonrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsRC committed May 19, 2024
1 parent 631de5c commit c4111f0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
14 changes: 14 additions & 0 deletions internal/rpc/jsonrpc/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ type ID struct {
value string
}

func NewID(value string, isNull ...bool) ID {
for _, v := range isNull {
if v {
return ID{
isNull: true,
value: "",
}
}
}
return ID{
value: value,
}
}

func (id ID) MarshalJSON() ([]byte, error) {
if id.isNull {
return []byte(`null`), nil
Expand Down
29 changes: 29 additions & 0 deletions internal/rpc/jsonrpc/jsonrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,3 +1056,32 @@ func TestResponseParseError(t *testing.T) {
require.NoError(t, err)
require.JSONEq(t, want, string(b))
}

// Returns an ID struct with the given value and isNull=false when no isNull parameter is passed
func TestNewID_NoIsNullParameter(t *testing.T) {
value := "test"
id := NewID(value)

if id.isNull {
t.Errorf("Expected isNull to be false, but got true")
}

if id.value != value {
t.Errorf("Expected value to be %s, but got %s", value, id.value)
}
}

// Returns an ID struct with the given value and isNull=false when isNull parameter is false
func TestNewID_IsNullParameterFalse(t *testing.T) {
value := "test"
isNull := false
id := NewID(value, isNull)

if id.isNull {
t.Errorf("Expected isNull to be false, but got true")
}

if id.value != value {
t.Errorf("Expected value to be %s, but got %s", value, id.value)
}
}
6 changes: 3 additions & 3 deletions internal/rpc/services/userservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func TestUserService_InvokeMethod(t *testing.T) {
},
args: args{
req: jsonrpc.Request{
ID: "123",
ID: jsonrpc.NewID("123"),
Method: "Users::GetUser",
},
},
Expand All @@ -305,7 +305,7 @@ func TestUserService_InvokeMethod(t *testing.T) {
args: args{
ctx: context.Background(),
req: jsonrpc.Request{
ID: "sadlk;fghj",
ID: jsonrpc.NewID("sadlk;fghj"),
Method: "Users::GetUsers",
},
},
Expand Down Expand Up @@ -376,7 +376,7 @@ func TestUserService_InvokeMethod(t *testing.T) {
},
args: args{
req: jsonrpc.Request{
ID: "42",
ID: jsonrpc.NewID("42"),
Method: "Users::badMethod",
},
},
Expand Down

0 comments on commit c4111f0

Please sign in to comment.