ref.go 7.2 KB

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