Skip to content
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

Merged
merged 2 commits into from
Nov 21, 2023
Merged

Add json data type #250

merged 2 commits into from
Nov 21, 2023

Conversation

amit-momin
Copy link
Contributor

Migrated the json data type from /core/services/pg/datatypes

Copy link
Collaborator

@jmank88 jmank88 left a 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?

Copy link
Contributor

@dimriou dimriou left a 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.

@jmank88
Copy link
Collaborator

jmank88 commented Nov 21, 2023

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 nil->null:

// 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

Copy link
Collaborator

@jmank88 jmank88 Nov 21, 2023

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

Copy link
Collaborator

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

Copy link
Contributor Author

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?

Copy link
Collaborator

@jmank88 jmank88 Nov 21, 2023

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.

Copy link
Contributor Author

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

@amit-momin amit-momin merged commit d7f28e9 into main Nov 21, 2023
@amit-momin amit-momin deleted the feature/add-json-type branch November 21, 2023 18:04
samsondav pushed a commit that referenced this pull request Jan 11, 2024
* Added json data type

* Moved json type to sqlutil package
ettec pushed a commit that referenced this pull request Mar 28, 2024
* Added json data type

* Moved json type to sqlutil package
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants