-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathspecs.go
157 lines (133 loc) · 3.3 KB
/
specs.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package specs
import "fmt"
type maturity string
const (
wip maturity = "wip"
draft maturity = "draft"
reliable maturity = "reliable"
stable maturity = "stable"
permanent maturity = "permanent"
deprecated maturity = "deprecated"
)
func (m maturity) isMature() bool {
switch m {
case reliable, stable, permanent:
return true
default:
return false
}
}
type Spec interface {
Name() string
IsEnabled() bool
IsMature() bool
Enable()
Disable()
}
type Leaf struct {
name string
maturity maturity
}
func (l Leaf) Name() string {
return l.name
}
// IsMature returns maturity status (only reliable, stable, or permanent specs will return true).
// See https://specs.ipfs.tech/meta/spec-for-specs/
func (l Leaf) IsMature() bool {
return l.maturity.isMature()
}
func (l Leaf) IsEnabled() bool {
// If the spec was explicitly enabled or disabled, use that.
// Otherwise, use the maturity level.
if enabled, ok := specEnabled[l]; ok {
return enabled
} else {
return l.IsMature()
}
}
func (s Leaf) Enable() {
specEnabled[s] = true
}
func (s Leaf) Disable() {
specEnabled[s] = false
}
type Collection struct {
name string
children []Spec
}
func (c Collection) Name() string {
return c.name
}
func (c Collection) IsEnabled() bool {
for _, s := range c.children {
if !s.IsEnabled() {
return false
}
}
return true
}
func (c Collection) IsMature() bool {
for _, s := range c.children {
if !s.IsMature() {
return false
}
}
return true
}
func (c Collection) Enable() {
for _, s := range c.children {
s.Enable()
}
}
func (c Collection) Disable() {
for _, s := range c.children {
s.Disable()
}
}
var (
TrustlessGatewayRaw = Leaf{"trustless-block-gateway", stable}
TrustlessGatewayCAR = Leaf{"trustless-car-gateway", stable}
TrustlessGatewayIPNS = Leaf{"trustless-ipns-gateway", stable}
TrustlessGateway = Collection{"trustless-gateway", []Spec{TrustlessGatewayRaw, TrustlessGatewayCAR, TrustlessGatewayIPNS}}
PathGatewayUnixFS = Leaf{"path-unixfs-gateway", stable}
PathGatewayIPNS = Leaf{"path-ipns-gateway", stable}
PathGatewayTAR = Leaf{"path-tar-gateway", stable}
PathGatewayDAG = Leaf{"path-dag-gateway", stable}
PathGatewayRaw = Leaf{"path-raw-gateway", stable}
PathGateway = Collection{"path-gateway", []Spec{PathGatewayUnixFS, PathGatewayIPNS, PathGatewayTAR, PathGatewayDAG, PathGatewayRaw}}
SubdomainGatewayIPFS = Leaf{"subdomain-ipfs-gateway", stable}
SubdomainGatewayIPNS = Leaf{"subdomain-ipns-gateway", stable}
SubdomainGateway = Collection{"subdomain-gateway", []Spec{SubdomainGatewayIPFS, SubdomainGatewayIPNS}}
DNSLinkGateway = Leaf{"dnslink-gateway", stable}
RedirectsFile = Leaf{"redirects-file", stable}
)
// All specs MUST be listed here.
var specs = []Spec{
TrustlessGatewayRaw,
TrustlessGatewayCAR,
TrustlessGatewayIPNS,
TrustlessGateway,
PathGatewayUnixFS,
PathGatewayIPNS,
PathGatewayTAR,
PathGatewayDAG,
PathGatewayRaw,
PathGateway,
SubdomainGatewayIPFS,
SubdomainGatewayIPNS,
SubdomainGateway,
DNSLinkGateway,
RedirectsFile,
}
var specEnabled = map[Spec]bool{}
func All() []Spec {
return specs
}
func FromString(name string) (Spec, error) {
for _, spec := range All() {
if spec.Name() == name {
return spec, nil
}
}
return nil, fmt.Errorf("unknown spec: %s", name)
}