-
Notifications
You must be signed in to change notification settings - Fork 9
/
name.go
113 lines (90 loc) · 2.35 KB
/
name.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package schema
import (
"path/filepath"
"strings"
"namespacelabs.dev/foundation/internal/uniquestrings"
)
type PackageName string
func (p PackageName) String() string { return string(p) }
func (p PackageName) Equals(s string) bool { return string(p) == s }
func (p PackageName) GetPackageName() string { return string(p) }
// Implements fnerrors.Location.
func (p PackageName) ErrorLocation() string { return string(p) }
func MakePackageName(str string) PackageName {
return PackageName(filepath.Clean(str))
}
type PackageList struct {
l uniquestrings.List
}
type PackageRefList struct {
l uniquestrings.List
refs []*PackageRef
}
func (pl *PackageList) Add(pkg PackageName) bool {
return pl.l.Add(pkg.String())
}
func (pl *PackageList) AddMultiple(pkgs ...PackageName) {
for _, pkg := range pkgs {
pl.l.Add(pkg.String())
}
}
func (pl PackageList) PackageNames() []PackageName {
o := pl.PackageNamesAsString()
r := make([]PackageName, len(o))
for k, v := range o {
r[k] = PackageName(v)
}
return r
}
func (pl PackageList) PackageNamesAsString() []string {
return pl.l.Strings()
}
func (pl PackageList) Has(pkg PackageName) bool {
return pl.l.Has(pkg.String())
}
func (pl PackageList) Len() int { return pl.l.Len() }
func (pl PackageList) Clone() PackageList {
return PackageList{l: pl.l.Clone()}
}
func (pl *PackageRefList) Add(ref *PackageRef) bool {
if pl.l.Add(ref.Canonical()) {
pl.refs = append(pl.refs, ref)
return true
}
return false
}
func (pl PackageRefList) Refs() []*PackageRef {
return pl.refs
}
func PackageNames(strs ...string) []PackageName {
o := make([]PackageName, len(strs))
for k, s := range strs {
o[k] = PackageName(s)
}
return o
}
func Strs(sch ...PackageName) []string {
o := make([]string, len(sch))
for k, s := range sch {
o[k] = s.String()
}
return o
}
func List(packages []PackageName) PackageList {
var pl PackageList
pl.AddMultiple(packages...)
return pl
}
func IsParent(moduleName string, sch PackageName) (string, bool) {
if sch.Equals(moduleName) {
return ".", true
}
name := string(sch)
if rel := strings.TrimPrefix(name, moduleName+"/"); rel != name {
return rel, true
}
return "", false
}