Skip to content

Instantly share code, notes, and snippets.

@wttw
Created April 20, 2017 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wttw/1102ce5e1ba39b3e391afaf64bbb07fc to your computer and use it in GitHub Desktop.
Save wttw/1102ce5e1ba39b3e391afaf64bbb07fc to your computer and use it in GitHub Desktop.
package model
import (
"database/sql/driver"
"encoding/json"
"errors"
"net"
)
//go:generate bash makemodel.sh
// Cidr is a cidr block
type Cidr net.IPNet
// Inet is an inet field
type Inet net.IPNet
// Json is an (unparsed) json field
type Json []byte
// Jsonb is an (unparsed) jsonb field
type Jsonb []byte
// Scan satisfies the sql.Scanner interface for Json.
func (j *Json) Scan(src interface{}) error {
buf, ok := src.([]byte)
if !ok {
return errors.New("invalid Json")
}
if buf == nil {
buf = []byte("{}")
}
*j = buf
return nil
}
// Value satisfies the driver.Valuer interface for Json.
func (j Json) Value() (driver.Value, error) {
if j == nil {
return "{}", nil
}
return string(j), nil
}
// Marshal a go structure into json
func (j *Json) Marshal(v interface{}) error {
bytes, err := json.Marshal(v)
if err != nil {
return err
}
*j = bytes
return nil
}
// Unmarshal from json into a go structure
func (j Json) Unmarshal(v interface{}) error {
return json.Unmarshal(j, v)
}
// Object returns the value decoded from json to an interface{}
func (j Json) Object() interface{} {
var ret interface{}
_ = json.Unmarshal(j, &ret)
return ret
}
// String satisfies Stringer
func (j Json) String() string {
return string(j)
}
// Scan satisfies the sql.Scanner interface for Jsonb.
func (j *Jsonb) Scan(src interface{}) error {
buf, ok := src.([]byte)
if !ok {
return errors.New("invalid Jsonb")
}
if buf == nil {
buf = []byte("{}")
}
*j = buf
return nil
}
// Value satisfies the driver.Valuer interface for Jsonb.
func (j Jsonb) Value() (driver.Value, error) {
if j == nil {
return "{}", nil
}
return string(j), nil
}
// Marshal a go structure into json
func (j *Jsonb) Marshal(v interface{}) error {
bytes, err := json.Marshal(v)
if err != nil {
return err
}
*j = bytes
return nil
}
// Unmarshal from json into a go structure
func (j Jsonb) Unmarshal(v interface{}) error {
return json.Unmarshal(j, v)
}
// Object returns the value decoded from json to an interface{}
func (j Jsonb) Object() interface{} {
var ret interface{}
_ = json.Unmarshal(j, &ret)
return ret
}
// String satisfies Stringer
func (j Jsonb) String() string {
return string(j)
}
// Scan satisfies the sql.Scanner interface for Cidr.
func (c *Cidr) Scan(src interface{}) error {
buf, ok := src.([]byte)
if !ok {
return errors.New("invalid Cidr")
}
_, ipnet, err := net.ParseCIDR(string(buf))
if err != nil {
return err
}
*c = Cidr(*ipnet)
return nil
}
// Value satisfies the driver.Valuer interface for Cidr.
func (c Cidr) Value() (driver.Value, error) {
n := net.IPNet(c)
return n.String(), nil
}
// String satisfies Stringer for Cidr
func (c Cidr) String() string {
n := net.IPNet(c)
return n.String()
}
// Scan satisfies the sql.Scanner interface for Inet.
func (c *Inet) Scan(src interface{}) error {
buf, ok := src.([]byte)
if !ok {
return errors.New("invalid Inet")
}
_, ipnet, err := net.ParseCIDR(string(buf))
if err != nil {
return err
}
*c = Inet(*ipnet)
return nil
}
// Value satisfies the driver.Valuer interface for Inet.
func (c Inet) Value() (driver.Value, error) {
n := net.IPNet(c)
return n.String(), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment