-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayer.go
More file actions
62 lines (53 loc) · 1.17 KB
/
layer.go
File metadata and controls
62 lines (53 loc) · 1.17 KB
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
53
54
55
56
57
58
59
60
61
62
package model
import (
"encoding/json"
"fmt"
)
// Layer is the five-way layer classification stamped by LayerClassifier
// (phase 2). Phase 1 detectors emit LayerUnknown; classification is deferred
// to phase 2's analyzer.LayerClassifier.
type Layer int
const (
LayerFrontend Layer = iota
LayerBackend
LayerInfra
LayerShared
LayerUnknown
)
var layerNames = [...]string{"frontend", "backend", "infra", "shared", "unknown"}
func (l Layer) String() string {
if int(l) < 0 || int(l) >= len(layerNames) {
return fmt.Sprintf("layer(%d)", int(l))
}
return layerNames[l]
}
func AllLayers() []Layer {
out := make([]Layer, len(layerNames))
for i := range layerNames {
out[i] = Layer(i)
}
return out
}
func ParseLayer(s string) (Layer, error) {
for i, name := range layerNames {
if name == s {
return Layer(i), nil
}
}
return 0, fmt.Errorf("unknown Layer: %q", s)
}
func (l Layer) MarshalJSON() ([]byte, error) {
return json.Marshal(l.String())
}
func (l *Layer) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
parsed, err := ParseLayer(s)
if err != nil {
return err
}
*l = parsed
return nil
}