-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
source_unit.go
46 lines (39 loc) · 1.26 KB
/
source_unit.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
package sources
import (
"encoding/json"
"fmt"
)
// Ensure CommonSourceUnit implements SourceUnit at compile time.
var _ SourceUnit = CommonSourceUnit{}
// CommonSourceUnit is a common implementation of SourceUnit that Sources can
// use instead of implementing their own types.
type CommonSourceUnit struct {
Kind SourceUnitKind `json:"kind,omitempty"`
ID string `json:"id"`
}
// SourceUnitID implements the SourceUnit interface.
func (c CommonSourceUnit) SourceUnitID() (string, SourceUnitKind) {
kind := SourceUnitKind("unit")
if c.Kind != "" {
kind = c.Kind
}
return c.ID, kind
}
func (c CommonSourceUnit) Display() string {
return c.ID
}
// CommonSourceUnitUnmarshaller is an implementation of SourceUnitUnmarshaller
// for the CommonSourceUnit. A source can embed this struct to gain the
// functionality of converting []byte to a CommonSourceUnit.
type CommonSourceUnitUnmarshaller struct{}
// UnmarshalSourceUnit implements the SourceUnitUnmarshaller interface.
func (c CommonSourceUnitUnmarshaller) UnmarshalSourceUnit(data []byte) (SourceUnit, error) {
var unit CommonSourceUnit
if err := json.Unmarshal(data, &unit); err != nil {
return nil, err
}
if unit.ID == "" {
return nil, fmt.Errorf("not a CommonSourceUnit")
}
return unit, nil
}