| 1 | package export |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "sort" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // Group is a group of projects, as an export carries it: its identifier, |
| 11 | // its name, and its aliases. A project's web URLs open with its group's |
| 12 | // name, /<name>/<project>/…, and a URL under an alias redirects to the |
| 13 | // same path under the name. The identifier never changes, and the |
| 14 | // projects of the group name it; no URL shows it. |
| 15 | type Group struct { |
| 16 | ID string `json:"group"` |
| 17 | Name string `json:"name"` |
| 18 | Aliases []string `json:"aliases,omitempty"` |
| 19 | } |
| 20 | |
| 21 | // Placement is where the instance places a project on the web: the |
| 22 | // identifier of its group, and its short aliases. /<alias>/… redirects to |
| 23 | // the same path under /<group name>/<project>/…. A project with no group |
| 24 | // has no web URL, and no aliases. |
| 25 | type Placement struct { |
| 26 | Group string |
| 27 | Aliases []string |
| 28 | } |
| 29 | |
| 30 | // Reserved are the first segments of the web server's own paths. No |
| 31 | // group's name or alias, and no project's alias, is one. |
| 32 | var Reserved = []string{"-"} |
| 33 | |
| 34 | // CheckNames holds groups, and the projects placed among them, to the |
| 35 | // rules the web routes by. Each group has an identifier, a name and |
| 36 | // aliases that are names, and no two groups share an identifier. Each |
| 37 | // placed project names a group that is there, and has aliases only when it |
| 38 | // has a group. Group names, group aliases and project aliases share one |
| 39 | // namespace, the first segment of a web URL: two that differ at most in |
| 40 | // ASCII case collide, a name given twice collides with itself, and none is |
| 41 | // a reserved first segment. A project's own name is unique within its |
| 42 | // group, which the project names it is declared under already are. |
| 43 | // |
| 44 | // The instance's declaration is held to these rules when it is read, and |
| 45 | // an export when it is built and read. |
| 46 | func CheckNames(groups []Group, placed map[string]Placement) error { |
| 47 | ids := map[string]Group{} |
| 48 | sorted := append([]Group{}, groups...) |
| 49 | sort.Slice(sorted, func(i, j int) bool { return sorted[i].ID < sorted[j].ID }) |
| 50 | claims := map[string]string{} |
| 51 | for _, r := range Reserved { |
| 52 | claims[strings.ToLower(r)] = "the web server's own paths" |
| 53 | } |
| 54 | claim := func(name, by string) error { |
| 55 | key := strings.ToLower(name) |
| 56 | if other, taken := claims[key]; taken { |
| 57 | return fmt.Errorf("%s and %s are both %q, ignoring case, and one first segment of a web URL names one group or project", other, by, name) |
| 58 | } |
| 59 | claims[key] = by |
| 60 | return nil |
| 61 | } |
| 62 | for _, g := range sorted { |
| 63 | if !ValidName(g.ID) { |
| 64 | return fmt.Errorf("a group's identifier is %q, which is not a name", g.ID) |
| 65 | } |
| 66 | if _, twice := ids[g.ID]; twice { |
| 67 | return fmt.Errorf("the group %s is declared twice", g.ID) |
| 68 | } |
| 69 | ids[g.ID] = g |
| 70 | names := append([]string{g.Name}, g.Aliases...) |
| 71 | for i, name := range names { |
| 72 | by := "the name of group " + g.ID |
| 73 | if i > 0 { |
| 74 | by = "alias " + strconv.Itoa(i-1) + " of group " + g.ID |
| 75 | } |
| 76 | if !ValidName(name) { |
| 77 | return fmt.Errorf("%s is %q, which is not a name", by, name) |
| 78 | } |
| 79 | if err := claim(name, by); err != nil { |
| 80 | return err |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | projects := make([]string, 0, len(placed)) |
| 85 | for name := range placed { |
| 86 | projects = append(projects, name) |
| 87 | } |
| 88 | sort.Strings(projects) |
| 89 | for _, project := range projects { |
| 90 | p := placed[project] |
| 91 | switch _, ok := ids[p.Group]; { |
| 92 | case p.Group == "" && len(p.Aliases) > 0: |
| 93 | return fmt.Errorf("the project %s has aliases and no group, so they have no URL to redirect to", project) |
| 94 | case p.Group != "" && !ok: |
| 95 | return fmt.Errorf("the project %s belongs to the group %q, which is not declared", project, p.Group) |
| 96 | } |
| 97 | for i, alias := range p.Aliases { |
| 98 | by := "alias " + strconv.Itoa(i) + " of project " + project |
| 99 | if !ValidName(alias) { |
| 100 | return fmt.Errorf("%s is %q, which is not a name", by, alias) |
| 101 | } |
| 102 | if err := claim(alias, by); err != nil { |
| 103 | return err |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | return nil |
| 108 | } |