#TIL 22 - Enum Type with PostgreSQL and sqlx | ProductsWay

#TIL 22 - Enum Type with PostgreSQL and sqlx

Oct 22, 2021 · Dung Huynh

What

Implement custom Scan and Value methods so sqlx can read and write PostgreSQL enum arrays in Go.

Why

PostgreSQL returns enum arrays in its array text format, which includes quoting and escaping rules. Delegate that format to lib/pq instead of splitting on commas, then convert between []string and your enum type.

How

import (
    "database/sql/driver"

    "github.com/lib/pq"
)

type ProjectSector string
type ProjectSectors []ProjectSector

func (ps *ProjectSectors) Scan(val interface{}) error {
    var values pq.StringArray
    if err := values.Scan(val); err != nil {
        return err
    }

    *ps = make(ProjectSectors, len(values))
    for i, value := range values {
        (*ps)[i] = ProjectSector(value)
    }
    return nil
}

func (ps ProjectSectors) Value() (driver.Value, error) {
    if ps == nil {
        return nil, nil
    }

    values := make(pq.StringArray, len(ps))
    for i, value := range ps {
        values[i] = string(value)
    }
    return values.Value()
}