ref.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. if !isValidPart(string(s[0])) {
  201. return
  202. }
  203. yieldValid := func(kind PartKind, value string) bool {
  204. if !isValidPart(value) {
  205. return false
  206. }
  207. return yield(kind, value)
  208. }
  209. state, j := Build, len(s)
  210. for i := len(s) - 1; i >= 0; i-- {
  211. switch s[i] {
  212. case '+':
  213. switch state {
  214. case Build:
  215. if !yieldValid(Build, s[i+1:j]) {
  216. return
  217. }
  218. state, j = Tag, i
  219. default:
  220. return
  221. }
  222. case ':':
  223. switch state {
  224. case Build, Tag:
  225. if yieldValid(Tag, s[i+1:j]) {
  226. state, j = Tag, i
  227. }
  228. state, j = Name, i
  229. default:
  230. return
  231. }
  232. case '/':
  233. switch state {
  234. case Name, Tag, Build:
  235. if !yieldValid(Name, s[i+1:j]) {
  236. return
  237. }
  238. state, j = Namespace, i
  239. case Namespace:
  240. if !yieldValid(Namespace, s[i+1:j]) {
  241. return
  242. }
  243. state, j = Domain, i
  244. default:
  245. yield(Invalid, "")
  246. return
  247. }
  248. }
  249. }
  250. // handle the first part based on final state
  251. switch state {
  252. case Domain:
  253. yieldValid(Domain, s[:j])
  254. case Namespace:
  255. yieldValid(Namespace, s[:j])
  256. default:
  257. yieldValid(Name, s[:j])
  258. }
  259. }
  260. }
  261. // Complete is the same as ParseRef(s).Complete().
  262. //
  263. // Future versions may be faster than calling ParseRef(s).Complete(), so if
  264. // need to know if a ref is complete and don't need the ref, use this
  265. // function.
  266. func Complete(s string) bool {
  267. // TODO(bmizerany): fast-path this with a quick scan withput
  268. // allocating strings
  269. return ParseRef(s).Complete()
  270. }
  271. func (r Ref) Valid() bool {
  272. // Name is required
  273. if !isValidPart(r.name) {
  274. return false
  275. }
  276. // Optional parts must be valid if present
  277. if r.domain != "" && !isValidPart(r.domain) {
  278. return false
  279. }
  280. if r.namespace != "" && !isValidPart(r.namespace) {
  281. return false
  282. }
  283. if r.tag != "" && !isValidPart(r.tag) {
  284. return false
  285. }
  286. if r.build != "" && !isValidPart(r.build) {
  287. return false
  288. }
  289. return true
  290. }
  291. // isValidPart returns true if given part is valid ascii [a-zA-Z0-9_\.-]
  292. func isValidPart(s string) bool {
  293. if s == "" {
  294. return false
  295. }
  296. for _, c := range []byte(s) {
  297. if c == '.' || c == '-' {
  298. return true
  299. }
  300. if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_' {
  301. continue
  302. } else {
  303. return false
  304. }
  305. }
  306. return true
  307. }