good morning!!!!

Skip to content
Snippets Groups Projects
Verified Commit 076f0f2f authored by a's avatar a
Browse files

cleanup

parent 6bdf3a7c
Branches
Tags
No related merge requests found
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"gfx.cafe/open/jrpc" "gfx.cafe/open/jrpc"
"gfx.cafe/open/jrpc/contrib/codecs" "gfx.cafe/open/jrpc/contrib/codecs"
"gfx.cafe/open/jrpc/contrib/jmux" "gfx.cafe/open/jrpc/contrib/jmux"
"gfx.cafe/open/jrpc/openrpc/out" "gfx.cafe/open/jrpc/contrib/openrpc/out"
) )
func main() { func main() {
......
...@@ -75,42 +75,11 @@ func (msg *Message) isNotification() bool { ...@@ -75,42 +75,11 @@ func (msg *Message) isNotification() bool {
return msg.ID == nil && len(msg.Method) > 0 return msg.ID == nil && len(msg.Method) > 0
} }
func (msg *Message) isCall() bool {
return msg.hasValidID() && len(msg.Method) > 0
}
func (msg *Message) isResponse() bool {
return msg.hasValidID() && len(msg.Method) == 0 && msg.Params == nil && (msg.Result != nil || msg.Error != nil)
}
func (msg *Message) hasValidID() bool {
return msg.ID != nil && !msg.ID.IsNull()
}
func (msg *Message) String() string { func (msg *Message) String() string {
b, _ := json.Marshal(msg) b, _ := json.Marshal(msg)
return string(b) return string(b)
} }
func (msg *Message) ErrorResponse(err error) *Message {
resp := ErrorMessage(err)
if resp.ID != nil {
resp.ID = msg.ID
}
return resp
}
func (msg *Message) response(result any) *Message {
// do a funny marshaling
enc, err := gojson.Marshal(result)
if err != nil {
return msg.ErrorResponse(err)
}
if len(enc) == 0 {
enc = []byte("null")
}
return &Message{ID: msg.ID, Result: enc}
}
// encapsulate json rpc error into struct // encapsulate json rpc error into struct
type JsonError struct { type JsonError struct {
Code int `json:"code"` Code int `json:"code"`
...@@ -133,28 +102,6 @@ func (err *JsonError) ErrorData() any { ...@@ -133,28 +102,6 @@ func (err *JsonError) ErrorData() any {
return err.Data return err.Data
} }
// error message produces json rpc message with error message
func ErrorMessage(err error) *Message {
if err == nil {
return nil
}
msg := &Message{
ID: NewNullIDPtr(),
Error: &JsonError{
Code: ErrorCodeDefault,
Message: err.Error(),
}}
ec, ok := err.(Error)
if ok {
msg.Error.Code = ec.ErrorCode()
}
de, ok := err.(DataError)
if ok {
msg.Error.Data = de.ErrorData()
}
return msg
}
// isBatch returns true when the first non-whitespace characters is '[' // isBatch returns true when the first non-whitespace characters is '['
func IsBatchMessage(raw json.RawMessage) bool { func IsBatchMessage(raw json.RawMessage) bool {
for _, c := range raw { for _, c := range raw {
......
...@@ -113,11 +113,6 @@ func NewNotification(ctx context.Context, method string, params any) *Request { ...@@ -113,11 +113,6 @@ func NewNotification(ctx context.Context, method string, params any) *Request {
return r return r
} }
func (r *Request) makeError(err error) *Message {
m := r.Msg()
return m.ErrorResponse(err)
}
func (r *Request) isNotification() bool { func (r *Request) isNotification() bool {
return r.ID == nil && len(r.Method) > 0 return r.ID == nil && len(r.Method) > 0
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment