-
Notifications
You must be signed in to change notification settings - Fork 0
/
affiliations.go
52 lines (42 loc) · 1.22 KB
/
affiliations.go
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
41
42
43
44
45
46
47
48
49
50
51
52
package models
import (
"database/sql/driver"
"errors"
"strings"
)
/*Affiliations contains all affiliations of a user. */
type Affiliations []string
/*NullAffiliations represents affiliations that may be null. */
type NullAffiliations struct {
Affiliations Affiliations
Valid bool //Valid is true if Affiliations is not NULL
}
/*Value constructs a SQL Value from NullAffiliations. */
func (affiliations NullAffiliations) Value() (driver.Value, error) {
if !affiliations.Valid {
return nil, nil
}
var str string
for _, affiliation := range affiliations.Affiliations {
str += `"` + affiliation + `",`
}
return driver.Value("{" + strings.TrimRight(str, ",") + "}"), nil
}
/*Scan constructs NullAffiliations from a SQL Value. */
func (affiliations *NullAffiliations) Scan(value interface{}) error {
if value == nil {
affiliations.Affiliations = []string{""}
affiliations.Valid = false
return nil
}
affiliations.Valid = true
switch value.(type) {
case string:
str := value.(string)
str = strings.TrimSpace(strings.ReplaceAll(strings.ReplaceAll(str, "{", ""), "}", ""))
affiliations.Affiliations = strings.Split(str, ",")
default:
return errors.New("incompatible type for Affiliations")
}
return nil
}