Skip to content

Commit

Permalink
Marked json data type as deprecated and cleaned up test vars
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-momin committed Nov 21, 2023
1 parent 5b30645 commit bc026b7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
60 changes: 60 additions & 0 deletions core/services/pg/datatypes/json.go
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 7 additions & 7 deletions core/services/vrf/v2/integration_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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(),
},
Expand All @@ -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(),
},
Expand All @@ -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(),
},
Expand All @@ -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,
Expand All @@ -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(),
})
Expand Down

0 comments on commit bc026b7

Please sign in to comment.