github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/pkg/path/pkg.go (about) 1 // Copyright 2020 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package path 16 17 import ( 18 "github.com/joomcode/cue/internal/core/adt" 19 "github.com/joomcode/cue/pkg/internal" 20 ) 21 22 func init() { 23 internal.Register("path", pkg) 24 } 25 26 var _ = adt.TopKind // in case the adt package isn't used 27 28 var ( 29 osRequired = &adt.Disjunction{ 30 Values: allOS, 31 } 32 33 unixDefault = &adt.Disjunction{ 34 NumDefaults: 1, 35 Values: allOS, 36 } 37 38 // windowsDefault is the default for VolumeName. 39 windowsDefault = &adt.Disjunction{ 40 NumDefaults: 1, 41 Values: append([]*adt.Vertex{ 42 newStr("windows"), 43 newStr("unix"), 44 newStr("plan9")}, unixOS...), 45 } 46 47 allOS = append([]*adt.Vertex{ 48 newStr("unix"), 49 newStr("windows"), 50 newStr("plan9"), 51 }, unixOS...) 52 53 // These all fall back to unix 54 unixOS = []*adt.Vertex{ 55 newStr("aix"), 56 newStr("android"), 57 newStr("darwin"), 58 newStr("dragonfly"), 59 newStr("freebsd"), 60 newStr("hurd"), 61 newStr("illumos"), 62 newStr("ios"), 63 newStr("js"), 64 newStr("linux"), 65 newStr("nacl"), 66 newStr("netbsd"), 67 newStr("openbsd"), 68 newStr("solaris"), 69 newStr("zos"), 70 } 71 ) 72 73 func newStr(s string) *adt.Vertex { 74 v := &adt.Vertex{} 75 v.SetValue(nil, adt.Finalized, &adt.String{Str: s}) 76 return v 77 } 78 79 var pkg = &internal.Package{ 80 CUE: `{ 81 Unix: "unix" 82 Windows: "windows" 83 Plan9: "plan9" 84 }`, 85 Native: []*internal.Builtin{{ 86 Name: "Split", 87 Params: []internal.Param{ 88 {Kind: adt.StringKind}, 89 {Kind: adt.StringKind, Value: unixDefault}, 90 }, 91 Result: adt.ListKind, 92 Func: func(c *internal.CallCtxt) { 93 path, os := c.String(0), c.String(1) 94 if c.Do() { 95 c.Ret = Split(path, OS(os)) 96 } 97 }, 98 }, { 99 Name: "SplitList", 100 Params: []internal.Param{ 101 {Kind: adt.StringKind}, 102 {Kind: adt.StringKind, Value: osRequired}, 103 }, 104 Result: adt.ListKind, 105 Func: func(c *internal.CallCtxt) { 106 path, os := c.String(0), c.String(1) 107 if c.Do() { 108 c.Ret = SplitList(path, OS(os)) 109 } 110 }, 111 }, { 112 Name: "Join", 113 Params: []internal.Param{ 114 {Kind: adt.ListKind}, 115 {Kind: adt.StringKind, Value: unixDefault}, 116 }, 117 Result: adt.StringKind, 118 Func: func(c *internal.CallCtxt) { 119 list, os := c.StringList(0), c.String(1) 120 if c.Do() { 121 c.Ret = Join(list, OS(os)) 122 } 123 }, 124 }, { 125 Name: "Match", 126 Params: []internal.Param{ 127 {Kind: adt.StringKind}, 128 {Kind: adt.StringKind, Value: unixDefault}, 129 }, 130 Result: adt.BoolKind, 131 Func: func(c *internal.CallCtxt) { 132 pattern, name, os := c.String(0), c.String(1), c.String(2) 133 if c.Do() { 134 c.Ret, c.Err = Match(pattern, name, OS(os)) 135 } 136 }, 137 }, { 138 Name: "Clean", 139 Params: []internal.Param{ 140 {Kind: adt.StringKind}, 141 {Kind: adt.StringKind, Value: unixDefault}, 142 }, 143 Result: adt.StringKind, 144 Func: func(c *internal.CallCtxt) { 145 path, os := c.String(0), c.String(1) 146 if c.Do() { 147 c.Ret = Clean(path, OS(os)) 148 } 149 }, 150 }, { 151 Name: "ToSlash", 152 Params: []internal.Param{ 153 {Kind: adt.StringKind}, 154 {Kind: adt.StringKind, Value: osRequired}, 155 }, 156 Result: adt.StringKind, 157 Func: func(c *internal.CallCtxt) { 158 path, os := c.String(0), c.String(1) 159 if c.Do() { 160 c.Ret = ToSlash(path, OS(os)) 161 } 162 }, 163 }, { 164 Name: "FromSlash", 165 Params: []internal.Param{ 166 {Kind: adt.StringKind}, 167 {Kind: adt.StringKind, Value: osRequired}, 168 }, 169 Result: adt.StringKind, 170 Func: func(c *internal.CallCtxt) { 171 path, os := c.String(0), c.String(1) 172 if c.Do() { 173 c.Ret = FromSlash(path, OS(os)) 174 } 175 }, 176 }, { 177 Name: "Ext", 178 Params: []internal.Param{ 179 {Kind: adt.StringKind}, 180 {Kind: adt.StringKind, Value: unixDefault}, 181 }, 182 Result: adt.StringKind, 183 Func: func(c *internal.CallCtxt) { 184 path, os := c.String(0), c.String(1) 185 if c.Do() { 186 c.Ret = Ext(path, OS(os)) 187 } 188 }, 189 }, { 190 Name: "Resolve", 191 Params: []internal.Param{ 192 {Kind: adt.StringKind}, 193 {Kind: adt.StringKind}, 194 {Kind: adt.StringKind, Value: unixDefault}, 195 }, 196 Result: adt.StringKind, 197 Func: func(c *internal.CallCtxt) { 198 dir, sub, os := c.String(0), c.String(1), c.String(2) 199 if c.Do() { 200 c.Ret = Resolve(dir, sub, OS(os)) 201 } 202 }, 203 }, { 204 Name: "Rel", 205 Params: []internal.Param{ 206 {Kind: adt.StringKind}, 207 {Kind: adt.StringKind}, 208 {Kind: adt.StringKind, Value: unixDefault}, 209 }, 210 Result: adt.StringKind, 211 Func: func(c *internal.CallCtxt) { 212 base, target, os := c.String(0), c.String(1), c.String(2) 213 if c.Do() { 214 c.Ret, c.Err = Rel(base, target, OS(os)) 215 } 216 }, 217 }, { 218 Name: "Base", 219 Params: []internal.Param{ 220 {Kind: adt.StringKind}, 221 {Kind: adt.StringKind, Value: unixDefault}, 222 }, 223 Result: adt.StringKind, 224 Func: func(c *internal.CallCtxt) { 225 path, os := c.String(0), c.String(1) 226 if c.Do() { 227 c.Ret = Base(path, OS(os)) 228 } 229 }, 230 }, { 231 Name: "Dir", 232 Params: []internal.Param{ 233 {Kind: adt.StringKind}, 234 {Kind: adt.StringKind, Value: unixDefault}, 235 }, 236 Result: adt.StringKind, 237 Func: func(c *internal.CallCtxt) { 238 path, os := c.String(0), c.String(1) 239 if c.Do() { 240 c.Ret = Dir(path, OS(os)) 241 } 242 }, 243 }, { 244 Name: "IsAbs", 245 Params: []internal.Param{ 246 {Kind: adt.StringKind}, 247 {Kind: adt.StringKind, Value: unixDefault}, 248 }, 249 Result: adt.BoolKind, 250 Func: func(c *internal.CallCtxt) { 251 path, os := c.String(0), c.String(1) 252 if c.Do() { 253 c.Ret = IsAbs(path, OS(os)) 254 } 255 }, 256 }, { 257 Name: "VolumeName", 258 Params: []internal.Param{ 259 {Kind: adt.StringKind}, 260 {Kind: adt.StringKind, Value: windowsDefault}, 261 }, 262 Result: adt.StringKind, 263 Func: func(c *internal.CallCtxt) { 264 path, os := c.String(0), c.String(1) 265 if c.Do() { 266 c.Ret = VolumeName(path, OS(os)) 267 } 268 }, 269 }}, 270 }