ref.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package blob
  2. import (
  3. "cmp"
  4. "iter"
  5. "slices"
  6. "strings"
  7. )
  8. type PartKind int
  9. // Levels of concreteness
  10. const (
  11. Invalid PartKind = iota
  12. Domain
  13. Namespace
  14. Name
  15. Tag
  16. Build
  17. )
  18. // Ref is an opaque reference to a blob.
  19. //
  20. // It is comparable and can be used as a map key.
  21. //
  22. // Users or Ref must check Valid before using it.
  23. type Ref struct {
  24. domain string
  25. namespace string
  26. name string
  27. tag string
  28. build string
  29. }
  30. // WithDomain returns a copy of r with the provided domain. If the provided
  31. // domain is empty, it returns the short, unqualified copy of r.
  32. func (r Ref) WithDomain(s string) Ref {
  33. r.domain = s
  34. return r
  35. }
  36. // WithNamespace returns a copy of r with the provided namespace. If the
  37. // provided namespace is empty, it returns the short, unqualified copy of r.
  38. func (r Ref) WithNamespace(s string) Ref {
  39. r.namespace = s
  40. return r
  41. }
  42. // WithName returns a copy of r with the provided name. If the provided
  43. // name is empty, it returns the short, unqualified copy of r.
  44. func (r Ref) WithName(s string) Ref {
  45. r.name = s
  46. return r
  47. }
  48. func (r Ref) WithTag(s string) Ref {
  49. r.tag = s
  50. return r
  51. }
  52. // WithBuild returns a copy of r with the provided build. If the provided
  53. // build is empty, it returns the short, unqualified copy of r.
  54. //
  55. // The build is normalized to uppercase.
  56. func (r Ref) WithBuild(s string) Ref {
  57. r.build = strings.ToUpper(s)
  58. return r
  59. }
  60. // Format returns a string representation of the ref with the given
  61. // concreteness. If a part is missing, it is replaced with a loud
  62. // placeholder.
  63. func (r Ref) Full() string {
  64. r.domain = cmp.Or(r.domain, "!(MISSING DOMAIN)")
  65. r.namespace = cmp.Or(r.namespace, "!(MISSING NAMESPACE)")
  66. r.name = cmp.Or(r.name, "!(MISSING NAME)")
  67. r.tag = cmp.Or(r.tag, "!(MISSING TAG)")
  68. r.build = cmp.Or(r.build, "!(MISSING BUILD)")
  69. return r.String()
  70. }
  71. func (r Ref) NameAndTag() string {
  72. r.domain = ""
  73. r.namespace = ""
  74. r.build = ""
  75. return r.String()
  76. }
  77. func (r Ref) NameTagAndBuild() string {
  78. r.domain = ""
  79. r.namespace = ""
  80. return r.String()
  81. }
  82. // String returns the fully qualified ref string.
  83. func (r Ref) String() string {
  84. var b strings.Builder
  85. if r.domain != "" {
  86. b.WriteString(r.domain)
  87. b.WriteString("/")
  88. }
  89. if r.namespace != "" {
  90. b.WriteString(r.namespace)
  91. b.WriteString("/")
  92. }
  93. b.WriteString(r.name)
  94. if r.tag != "" {
  95. b.WriteString(":")
  96. b.WriteString(r.tag)
  97. }
  98. if r.build != "" {
  99. b.WriteString("+")
  100. b.WriteString(r.build)
  101. }
  102. return b.String()
  103. }
  104. // Complete returns true if the ref is valid and has no empty parts.
  105. func (r Ref) Complete() bool {
  106. return r.Valid() && !slices.Contains(r.Parts(), "")
  107. }
  108. func (r Ref) CompleteWithoutBuild() bool {
  109. return r.Valid() && !slices.Contains(r.Parts()[:Tag], "")
  110. }
  111. // Less returns true if r is less concrete than o; false otherwise.
  112. func (r Ref) Less(o Ref) bool {
  113. rp := r.Parts()
  114. op := o.Parts()
  115. for i := range rp {
  116. if rp[i] < op[i] {
  117. return true
  118. }
  119. }
  120. return false
  121. }
  122. // Parts returns the parts of the ref in order of concreteness.
  123. //
  124. // The length of the returned slice is always 5.
  125. func (r Ref) Parts() []string {
  126. return []string{
  127. r.domain,
  128. r.namespace,
  129. r.name,
  130. r.tag,
  131. r.build,
  132. }
  133. }
  134. func (r Ref) Domain() string { return r.namespace }
  135. func (r Ref) Namespace() string { return r.namespace }
  136. func (r Ref) Name() string { return r.name }
  137. func (r Ref) Tag() string { return r.tag }
  138. func (r Ref) Build() string { return r.build }
  139. // ParseRef parses a ref string into a Ref. A ref string is a name, an
  140. // optional tag, and an optional build, separated by colons and pluses.
  141. //
  142. // The name must be valid ascii [a-zA-Z0-9_].
  143. // The tag must be valid ascii [a-zA-Z0-9_].
  144. // The build must be valid ascii [a-zA-Z0-9_].
  145. //
  146. // It returns then zero value if the ref is invalid.
  147. //
  148. // // Valid Examples:
  149. // ParseRef("mistral:latest") returns ("mistral", "latest", "")
  150. // ParseRef("mistral") returns ("mistral", "", "")
  151. // ParseRef("mistral:30B") returns ("mistral", "30B", "")
  152. // ParseRef("mistral:7b") returns ("mistral", "7b", "")
  153. // ParseRef("mistral:7b+Q4_0") returns ("mistral", "7b", "Q4_0")
  154. // ParseRef("mistral+KQED") returns ("mistral", "latest", "KQED")
  155. // ParseRef(".x.:7b+Q4_0:latest") returns (".x.", "7b", "Q4_0")
  156. // ParseRef("-grok-f.oo:7b+Q4_0") returns ("-grok-f.oo", "7b", "Q4_0")
  157. //
  158. // // Invalid Examples:
  159. // ParseRef("m stral") returns ("", "", "") // zero
  160. // ParseRef("... 129 chars ...") returns ("", "", "") // zero
  161. func ParseRef(s string) Ref {
  162. var r Ref
  163. for kind, part := range Parts(s) {
  164. switch kind {
  165. case Domain:
  166. r = r.WithDomain(part)
  167. case Namespace:
  168. r = r.WithNamespace(part)
  169. case Name:
  170. r.name = part
  171. case Tag:
  172. r = r.WithTag(part)
  173. case Build:
  174. r = r.WithBuild(part)
  175. case Invalid:
  176. return Ref{}
  177. }
  178. }
  179. if !r.Valid() {
  180. return Ref{}
  181. }
  182. return r
  183. }
  184. // Parts returns a sequence of the parts of a ref string from most specific
  185. // to least specific.
  186. //
  187. // It normalizes the input string by removing "http://" and "https://" only.
  188. // No other normalization is done.
  189. func Parts(s string) iter.Seq2[PartKind, string] {
  190. return func(yield func(PartKind, string) bool) {
  191. if strings.HasPrefix(s, "http://") {
  192. s = s[len("http://"):]
  193. }
  194. if strings.HasPrefix(s, "https://") {
  195. s = s[len("https://"):]
  196. }
  197. if len(s) > 255 || len(s) == 0 {
  198. return
  199. }
  200. yieldValid := func(kind PartKind, value string) bool {
  201. if !isValidPart(value) {
  202. yield(Invalid, "")
  203. return false
  204. }
  205. return yield(kind, value)
  206. }
  207. state, j := Build, len(s)
  208. for i := len(s) - 1; i >= 0; i-- {
  209. switch s[i] {
  210. case '+':
  211. switch state {
  212. case Build:
  213. if !yieldValid(Build, s[i+1:j]) {
  214. return
  215. }
  216. state, j = Tag, i
  217. default:
  218. return
  219. }
  220. case ':':
  221. switch state {
  222. case Build, Tag:
  223. if yieldValid(Tag, s[i+1:j]) {
  224. state, j = Tag, i
  225. }
  226. state, j = Name, i
  227. default:
  228. return
  229. }
  230. case '/':
  231. switch state {
  232. case Name, Tag, Build:
  233. if !yieldValid(Name, s[i+1:j]) {
  234. return
  235. }
  236. state, j = Namespace, i
  237. case Namespace:
  238. if !yieldValid(Namespace, s[i+1:j]) {
  239. return
  240. }
  241. state, j = Domain, i
  242. case Domain:
  243. // domain is not allowed to have slashes
  244. yield(Invalid, "")
  245. return
  246. default:
  247. return
  248. }
  249. }
  250. }
  251. // handle the first part based on final state
  252. switch state {
  253. case Domain:
  254. yieldValid(Domain, s[:j])
  255. case Namespace:
  256. yieldValid(Namespace, s[:j])
  257. default:
  258. yieldValid(Name, s[:j])
  259. }
  260. }
  261. }
  262. // Complete is the same as ParseRef(s).Complete().
  263. //
  264. // Future versions may be faster than calling ParseRef(s).Complete(), so if
  265. // need to know if a ref is complete and don't need the ref, use this
  266. // function.
  267. func Complete(s string) bool {
  268. // TODO(bmizerany): fast-path this with a quick scan withput
  269. // allocating strings
  270. return ParseRef(s).Complete()
  271. }
  272. func (r Ref) Valid() bool {
  273. // Name is required
  274. if !isValidPart(r.name) {
  275. return false
  276. }
  277. // Optional parts must be valid if present
  278. if r.domain != "" && !isValidPart(r.domain) {
  279. return false
  280. }
  281. if r.namespace != "" && !isValidPart(r.namespace) {
  282. return false
  283. }
  284. if r.tag != "" && !isValidPart(r.tag) {
  285. return false
  286. }
  287. if r.build != "" && !isValidPart(r.build) {
  288. return false
  289. }
  290. return true
  291. }
  292. // isValidPart returns true if given part is valid ascii [a-zA-Z0-9_\.-]
  293. func isValidPart(s string) bool {
  294. if s == "" {
  295. return false
  296. }
  297. for _, c := range []byte(s) {
  298. if c == '.' || c == '-' {
  299. return true
  300. }
  301. if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_' {
  302. continue
  303. } else {
  304. return false
  305. }
  306. }
  307. return true
  308. }