Files
gas-stack/pkg/db/vsc.go
~wispem-wantex 936df98cc4
All checks were successful
CI / build-docker (push) Successful in 2s
CI / build-docker-bootstrap (push) Has been skipped
CI / release-test (push) Successful in 2m17s
db: add VSC
2026-10-23 17:16:35 -07:00

52 lines
903 B
Go

package db
import (
"database/sql/driver"
"encoding/json"
"errors"
"strings"
)
const separator = "\x1f"
type VSC []string
// MarshalJSON replaces `nil` with `[]` for hygiene.
func (vs VSC) MarshalJSON() ([]byte, error) {
if vs == nil {
vs = VSC{}
}
return json.Marshal([]string(vs)) //nolint: wrapcheck // Passthrough
}
func (vs VSC) Value() (driver.Value, error) {
fields := make([]string, len(vs))
for i, v := range vs {
fields[i] = strings.ReplaceAll(v, separator, "")
}
return strings.Join(fields, separator), nil
}
var ErrWrongVSCType = errors.New("incompatible DB type for VSC")
func (vs *VSC) Scan(src any) error {
var s string
switch v := src.(type) {
case string:
s = v
case []byte:
s = string(v)
case nil:
*vs = nil
return nil
default:
return ErrWrongVSCType
}
if s == "" {
*vs = VSC{}
return nil
}
*vs = strings.Split(s, separator)
return nil
}