ref.go 7.2 KB

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