Skip to content

Commit

Permalink
feat: jsonb and consistent constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
23doors committed Aug 13, 2020
1 parent fc544df commit 91b7989
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 9 deletions.
17 changes: 16 additions & 1 deletion database/fields/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@ import (
"github.com/jackc/pgtype"
)

var (
DateFormat = "2006-01-02"
)

type Date struct {
pgtype.Date
}

func NewDate(val interface{}) Date {
var d pgtype.Date

err := d.Set(val)
if err != nil {
panic(err)
}

return Date{Date: d}
}

// Value is used on value in go-pg, pass it to pointer version.
func (d Date) Value() (driver.Value, error) {
return d.Date.Value()
Expand All @@ -22,7 +37,7 @@ func (d *Date) IsNull() bool {
}

func (d *Date) String() string {
return d.Time.UTC().Format(DateTimeFormat)
return d.Time.UTC().Format(DateFormat)
}

func (d *Date) MarshalJSON() ([]byte, error) {
Expand Down
11 changes: 9 additions & 2 deletions database/fields/hstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ type Hstore struct {
pgtype.Hstore
}

func NewHstore() Hstore {
return Hstore{Hstore: pgtype.Hstore{Map: make(map[string]pgtype.Text), Status: pgtype.Present}}
func NewHstore(val interface{}) Hstore {
var h pgtype.Hstore

err := h.Set(val)
if err != nil {
panic(err)
}

return Hstore{Hstore: h}
}

// Value is used on value in go-pg, pass it to pointer version.
Expand Down
13 changes: 12 additions & 1 deletion database/fields/json.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fields
package fields //nolint: dupl

import (
"database/sql/driver"
Expand All @@ -12,6 +12,17 @@ type JSON struct {
Data interface{}
}

func NewJSON(val interface{}) JSON {
var j pgtype.JSON

err := j.Set(val)
if err != nil {
panic(err)
}

return JSON{JSON: j}
}

// Value implements the database/sql/driver Valuer interface.
func (j JSON) Value() (driver.Value, error) {
if j.Data != nil {
Expand Down
63 changes: 63 additions & 0 deletions database/fields/jsonb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package fields //nolint: dupl

import (
"database/sql/driver"
"encoding/json"

"github.com/jackc/pgtype"
)

type JSONB struct {
pgtype.JSONB
Data interface{}
}

func NewJSONB(val interface{}) JSONB {
var j pgtype.JSONB

err := j.Set(val)
if err != nil {
panic(err)
}

return JSONB{JSONB: j}
}

// Value implements the database/sql/driver Valuer interface.
func (j JSONB) Value() (driver.Value, error) {
if j.Data != nil {
b, e := json.Marshal(j.Data)
return string(b), e
}

return j.JSONB.Value()
}

func (j *JSONB) Get() interface{} {
if j.Data == nil && !j.IsNull() {
j.Data = j.JSONB.Get()
}

return j.Data
}

// Scan implements the database/sql Scanner interface.
func (j *JSONB) Scan(src interface{}) error {
err := j.JSONB.Scan(src)
j.Data = nil

return err
}

// IsNull returns true if underlying value is null.
func (j *JSONB) IsNull() bool {
return j.JSONB.Status == pgtype.Null
}

func (j *JSONB) MarshalJSON() ([]byte, error) {
if j.Data != nil {
return json.Marshal(j.Data)
}

return j.Bytes, nil
}
12 changes: 7 additions & 5 deletions database/fields/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fields
import (
"database/sql/driver"
"fmt"
"time"

"github.com/jackc/pgtype"
)
Expand All @@ -17,12 +16,15 @@ type Time struct {
pgtype.Timestamptz
}

func NewTime(t *time.Time) Time {
if t == nil || t.IsZero() {
return Time{Timestamptz: pgtype.Timestamptz{Status: pgtype.Null}}
func NewTime(val interface{}) Time {
var t pgtype.Timestamptz

err := t.Set(val)
if err != nil {
panic(err)
}

return Time{Timestamptz: pgtype.Timestamptz{Time: t.UTC(), Status: pgtype.Present}}
return Time{Timestamptz: t}
}

// Value is used on value in go-pg, pass it to pointer version.
Expand Down

0 comments on commit 91b7989

Please sign in to comment.