File size: 737 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package calsub

import (
	"database/sql"
	"database/sql/driver"
	"encoding/json"
	"fmt"
)

// SubscriptionConfig is the configuration for a calendar subscription.
type SubscriptionConfig struct {
	ReminderMinutes []int
	FullSchedule    bool
}

var (
	_ = driver.Valuer(SubscriptionConfig{})
	_ = sql.Scanner(&SubscriptionConfig{})
)

func (scfg SubscriptionConfig) Value() (driver.Value, error) {
	data, err := json.Marshal(scfg)
	if err != nil {
		return nil, err
	}

	return data, nil
}

func (scfg *SubscriptionConfig) Scan(v interface{}) error {
	switch v := v.(type) {
	case []byte:
		return json.Unmarshal(v, scfg)
	case string:
		return json.Unmarshal([]byte(v), scfg)
	default:
		return fmt.Errorf("unsupported type %T", v)
	}
}