ref.go 6.7 KB

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