-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: jsonb and consistent constructor
- Loading branch information
Showing
5 changed files
with
107 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters