Added type for handling json types
This commit is contained in:
@@ -4,12 +4,14 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
|
"git.kundeng.us/phoenix/textsender-models/tx0/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MessageEventResponse struct {
|
type MessageEventResponse struct {
|
||||||
Id uuid.UUID `json:"id,omitempty"`
|
Id uuid.UUID `json:"id,omitempty"`
|
||||||
ScheduledMessageEventId uuid.UUID `json:"scheduled_message_event_id,omitempty"`
|
ScheduledMessageEventId uuid.UUID `json:"scheduled_message_event_id,omitempty"`
|
||||||
Response []byte `json:"response"`
|
Response types.JSONB `json:"response"`
|
||||||
UserId uuid.UUID `json:"user_id,omitempty"`
|
UserId uuid.UUID `json:"user_id,omitempty"`
|
||||||
Sent time.Time `json:"sent"`
|
Sent time.Time `json:"sent"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type JSONB json.RawMessage
|
||||||
|
|
||||||
|
// Scan implements sql.Scanner
|
||||||
|
func (j *JSONB) Scan(value interface{}) error {
|
||||||
|
if value == nil {
|
||||||
|
*j = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var data []byte
|
||||||
|
switch v := value.(type) {
|
||||||
|
case []byte:
|
||||||
|
// pgx may pass data as []byte in binary format
|
||||||
|
data = v
|
||||||
|
case string:
|
||||||
|
// pgx may pass data as string in text format
|
||||||
|
data = []byte(v)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported type: %T", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
*j = JSONB(data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value implements driver.Valuer
|
||||||
|
func (j JSONB) Value() (driver.Value, error) {
|
||||||
|
if j == nil {
|
||||||
|
return nil, nil
|
||||||
|
} else {
|
||||||
|
return []byte(j), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user