diff --git a/core/services/pg/datatypes/json.go b/core/services/pg/datatypes/json.go new file mode 100644 index 00000000000..ca21788b207 --- /dev/null +++ b/core/services/pg/datatypes/json.go @@ -0,0 +1,60 @@ +package datatypes + +import ( + "database/sql/driver" + "encoding/json" + "errors" + "fmt" +) + +// Deprecated: JSON data type was moved to chainlink-common which should be used instead +// JSON defined JSON data type, need to implements driver.Valuer, sql.Scanner interface +type JSON json.RawMessage + +// Value return json value, implement driver.Valuer interface +func (j JSON) Value() (driver.Value, error) { + if len(j) == 0 { + return nil, nil + } + bytes, err := json.RawMessage(j).MarshalJSON() + return string(bytes), err +} + +// Scan scan value into Jsonb, implements sql.Scanner interface +func (j *JSON) Scan(value interface{}) error { + if value == nil { + *j = JSON("null") + return nil + } + var bytes []byte + switch v := value.(type) { + case []byte: + bytes = v + case string: + bytes = []byte(v) + default: + return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value)) + } + + result := json.RawMessage{} + err := json.Unmarshal(bytes, &result) + *j = JSON(result) + return err +} + +// MarshalJSON to output non base64 encoded []byte +func (j JSON) MarshalJSON() ([]byte, error) { + return json.RawMessage(j).MarshalJSON() +} + +// UnmarshalJSON to deserialize []byte +func (j *JSON) UnmarshalJSON(b []byte) error { + result := json.RawMessage{} + err := result.UnmarshalJSON(b) + *j = JSON(result) + return err +} + +func (j JSON) String() string { + return string(j) +} diff --git a/core/services/vrf/v2/integration_v2_test.go b/core/services/vrf/v2/integration_v2_test.go index faba121737b..f57cb640f67 100644 --- a/core/services/vrf/v2/integration_v2_test.go +++ b/core/services/vrf/v2/integration_v2_test.go @@ -2038,13 +2038,13 @@ func TestStartingCountsV1(t *testing.T) { } md1, err := json.Marshal(&m1) require.NoError(t, err) - md1_ := sqlutil.JSON(md1) + md1SQL := sqlutil.JSON(md1) reqID2 := utils.PadByteToHash(0x11) m2 := txmgr.TxMeta{ RequestID: &reqID2, } md2, err := json.Marshal(&m2) - md2_ := sqlutil.JSON(md2) + md2SQL := sqlutil.JSON(md2) require.NoError(t, err) chainID := utils.NewBig(testutils.SimulatedChainID) confirmedTxes := []txmgr.Tx{ @@ -2068,7 +2068,7 @@ func TestStartingCountsV1(t *testing.T) { InitialBroadcastAt: &b, CreatedAt: b, State: txmgrcommon.TxConfirmed, - Meta: &md1_, + Meta: &md1SQL, EncodedPayload: []byte{}, ChainID: chainID.ToInt(), }, @@ -2080,7 +2080,7 @@ func TestStartingCountsV1(t *testing.T) { InitialBroadcastAt: &b, CreatedAt: b, State: txmgrcommon.TxConfirmed, - Meta: &md2_, + Meta: &md2SQL, EncodedPayload: []byte{}, ChainID: chainID.ToInt(), }, @@ -2092,7 +2092,7 @@ func TestStartingCountsV1(t *testing.T) { InitialBroadcastAt: &b, CreatedAt: b, State: txmgrcommon.TxConfirmed, - Meta: &md2_, + Meta: &md2SQL, EncodedPayload: []byte{}, ChainID: chainID.ToInt(), }, @@ -2105,7 +2105,7 @@ func TestStartingCountsV1(t *testing.T) { RequestID: &reqID3, }) require.NoError(t, err2) - md_ := sqlutil.JSON(md) + mdSQL := sqlutil.JSON(md) newNonce := evmtypes.Nonce(i + 1) unconfirmedTxes = append(unconfirmedTxes, txmgr.Tx{ Sequence: &newNonce, @@ -2115,7 +2115,7 @@ func TestStartingCountsV1(t *testing.T) { State: txmgrcommon.TxUnconfirmed, BroadcastAt: &b, InitialBroadcastAt: &b, - Meta: &md_, + Meta: &mdSQL, EncodedPayload: []byte{}, ChainID: chainID.ToInt(), })