Gorm: how to support tsrange[]

Created on 11 Aug 2018  路  4Comments  路  Source: go-gorm/gorm

Do we support field parse function?

question

Most helpful comment

it is possible to specify column type in GORM.

type Duration struct {
    From time.Time
    To   time.Time
}

func (d *Duration) Scan(src interface{}) error {
    panic("implement me")
}

func (d Duration) Value() (driver.Value, error) {
    return fmt.Sprintf(
        "[%s, %s)",
        d.From.Format("2006-01-02 15:04"),
        d.To.Format("2006-01-02 15:04"),
    ), nil
}

type Booking struct {
    ID        int       `gorm:"primary_key"`
    Duration  Duration  `gorm:"type:tsrange;column:duration;not null"` 
}

All 4 comments

Can u please provide more detail please.

in postgres, how did this orm map range type to golang?
I didn't found any document related to this.

it is possible to specify column type in GORM.

type Duration struct {
    From time.Time
    To   time.Time
}

func (d *Duration) Scan(src interface{}) error {
    panic("implement me")
}

func (d Duration) Value() (driver.Value, error) {
    return fmt.Sprintf(
        "[%s, %s)",
        d.From.Format("2006-01-02 15:04"),
        d.To.Format("2006-01-02 15:04"),
    ), nil
}

type Booking struct {
    ID        int       `gorm:"primary_key"`
    Duration  Duration  `gorm:"type:tsrange;column:duration;not null"` 
}

@rustjason fixed?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

corvinusy picture corvinusy  路  3Comments

easonlin404 picture easonlin404  路  3Comments

youtwo123 picture youtwo123  路  3Comments

fieryorc picture fieryorc  路  3Comments

superwf picture superwf  路  3Comments