-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add json data type #250
Add json data type #250
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to call Marshall and unmarshal explicitly on rawmessage? Aren't those no-ops?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would test Jordan's comment about marshal & unmarshal methods but otherwise looks ok.
Oh the implementations are basically no-ops anyways - just support for // MarshalJSON returns m as the JSON encoding of m.
func (m RawMessage) MarshalJSON() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}
return m, nil
}
// UnmarshalJSON sets *m to a copy of data.
func (m *RawMessage) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
*m = append((*m)[0:0], data...)
return nil
} https://cs.opensource.google/go/go/+/refs/tags/go1.21.4:src/encoding/json/stream.go;l=260-275 |
pkg/utils/json/json.go
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like having a separate package for json related things, but it has two problems:
- if you import std lib also, then you have to alias one of them as something other than
json
- redundant naming from outside the package:
json.JSON
What about something like package jsonutil
? And we could reuse the name RawMessage
jsonutil.RawMessage
Or maybe this makes sense in the sqlutil
package, in which case the JSON
name is probably fine.
sqlutil.JSON
I'm thinking this feels more SQL related 🤔
Edit: is the core side change ready? That would help give some perspective.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have this PR open but it still needs the changes to point to the JSON type here in chainlink-common
. But any place I've replaced datatypes.JSON
-> json.RawMessage
is where I'd use this. I think naming the package jsonutil
makes sense. It doesn't seem completely SQL related since it's used for things like the TxMeta type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can temporarily use the hash from this PR to show passing tests on the core side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't seem completely SQL related since it's used for things like the TxMeta type.
The whole purpose of the type is to extend support for SQL though:
// JSON defined JSON data type, need to implements driver.Valuer, sql.Scanner interface
type JSON json.RawMessage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true. For the current changes, it's using these methods under the hood so I can also get behind putting this under sqlutil
. But I could see someone using this type just for RawMessage
without the extended support being needed. Even though RawMessage
could be used directly from encoding/json
, would we want to use this type here going forward for consistency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though RawMessage could be used directly from encoding/json, would we want to use this type here going forward for consistency?
No, I think each case should use whatever fits best. They are still compatible when defined this way, and can even be converted without any runtime cost.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok in that case I'll move this under the sqlutil
package
* Added json data type * Moved json type to sqlutil package
* Added json data type * Moved json type to sqlutil package
Migrated the json data type from
/core/services/pg/datatypes