ref.go 7.4 KB

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