gunk-dev/the-valley

groups.go

source823c1f15e175main's published tip history
5.5 KiB
1package export
2
3import (
4 "context"
5 "fmt"
6 "slices"
7 "strconv"
8 "strings"
9)
10
11// Group is a group of projects, as an export carries it: its identifier,
12// its name, and its aliases. A project's web URLs open with its group's
13// name, /<name>/<project's web name>/…, and a URL under an alias
14// redirects to the same path under the name. The identifier never
15// changes, and the projects of the group name it; no URL shows it.
16type Group struct {
17 ID string `json:"group"`
18 Name string `json:"name"`
19 Aliases []string `json:"aliases,omitempty"`
20}
21
22// Placement is where the instance places a project on the web: the
23// identifier of its group, its web name within the group, its former web
24// names there, and its short aliases. Its web URLs are
25// /<group name>/<name>/…. /<group name>/<former name>/… and /<alias>/…
26// redirect to the same path under them. A project with no group has no
27// web URL, and no web name, former names or aliases.
28type Placement struct {
29 Group string
30 Name string
31 FormerNames []string
32 Aliases []string
33}
34
35// Placed is a project, by its identifier, and where the instance places
36// it.
37type Placed struct {
38 Project string
39 Placement
40}
41
42// Sorted is placements, by project, in project order, as CheckNames takes
43// them.
44func Sorted(placements map[string]Placement) []Placed {
45 out := make([]Placed, 0, len(placements))
46 for project, p := range placements {
47 out = append(out, Placed{project, p})
48 }
49 slices.SortFunc(out, func(a, b Placed) int { return strings.Compare(a.Project, b.Project) })
50 return out
51}
52
53// Reserved are the first segments of the web server's own paths. No
54// group's name or alias, and no project's alias, is one.
55var Reserved = []string{"-"}
56
57// CheckNames holds groups, and the projects placed among them, to the
58// rules the web routes by. groups are in identifier order, and placed in
59// project order, each once. Each group's identifier, name and aliases are
60// names. Each placed project's identifier is a name, and a project in a
61// group has a web name that is a name, while one in no group has no web
62// name, no former names and no aliases. Each group a project names is
63// there.
64//
65// The names share two namespaces. Group names, group aliases and project
66// aliases share the first segment of a web URL. The web names and former
67// names of one group's projects share the second segment of that group's
68// URLs. In each namespace, two names that differ at most in ASCII case
69// collide, and a name given twice collides with itself. No first segment
70// is a reserved one. Two groups' projects never collide, so each group
71// may hold a project named app.
72//
73// CheckNames checks ctx before each name it reads, so the work between
74// two checks is bounded by the length of a name. The instance's
75// declaration is held to these rules when it is read, and an export when
76// it is built and read.
77func CheckNames(ctx context.Context, groups []Group, placed []Placed) error {
78 first := map[string]string{}
79 for _, r := range Reserved {
80 first[strings.ToLower(r)] = "the web server's own paths"
81 }
82 second := map[string]map[string]string{}
83 // claim checks ctx, then claims name for by in names. segment says
84 // what the namespace is, for the message.
85 claim := func(names map[string]string, name, by, segment string) error {
86 if err := ctx.Err(); err != nil {
87 return err
88 }
89 if !ValidName(name) {
90 return fmt.Errorf("%s is %q, which is not a name", by, name)
91 }
92 key := strings.ToLower(name)
93 if other, taken := names[key]; taken {
94 return fmt.Errorf("%s and %s are both %q, ignoring case: %s", other, by, name, segment)
95 }
96 names[key] = by
97 return nil
98 }
99 const firstSegment = "group names and aliases must be unique"
100 for i, g := range groups {
101 if err := ctx.Err(); err != nil {
102 return err
103 }
104 if !ValidName(g.ID) {
105 return fmt.Errorf("a group's identifier is %q, which is not a name", g.ID)
106 }
107 if i > 0 && groups[i-1].ID >= g.ID {
108 return fmt.Errorf("the group %s is listed out of order or twice", g.ID)
109 }
110 second[g.ID] = map[string]string{}
111 if err := claim(first, g.Name, "the name of group "+g.ID, firstSegment); err != nil {
112 return err
113 }
114 for j, alias := range g.Aliases {
115 if err := claim(first, alias, "alias "+strconv.Itoa(j)+" of group "+g.ID, firstSegment); err != nil {
116 return err
117 }
118 }
119 }
120 for i, p := range placed {
121 if err := ctx.Err(); err != nil {
122 return err
123 }
124 if !ValidName(p.Project) {
125 return fmt.Errorf("a project's identifier is %q, which is not a name", p.Project)
126 }
127 if i > 0 && placed[i-1].Project >= p.Project {
128 return fmt.Errorf("the project %s is placed out of order or twice", p.Project)
129 }
130 names, declared := second[p.Group]
131 switch {
132 case p.Group == "" && (p.Name != "" || len(p.FormerNames) > 0 || len(p.Aliases) > 0):
133 return fmt.Errorf("the project %s has a web name, former names or aliases and no group", p.Project)
134 case p.Group == "":
135 continue
136 case !declared:
137 return fmt.Errorf("the project %s belongs to the group %q, which is not declared", p.Project, p.Group)
138 }
139 segment := "web names and former names must be unique in group " + p.Group
140 if err := claim(names, p.Name, "the web name of project "+p.Project, segment); err != nil {
141 return err
142 }
143 for j, former := range p.FormerNames {
144 if err := claim(names, former, "former name "+strconv.Itoa(j)+" of project "+p.Project, segment); err != nil {
145 return err
146 }
147 }
148 for j, alias := range p.Aliases {
149 if err := claim(first, alias, "alias "+strconv.Itoa(j)+" of project "+p.Project, firstSegment); err != nil {
150 return err
151 }
152 }
153 }
154 return nil
155}