path.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package model
  2. import (
  3. "cmp"
  4. "iter"
  5. "slices"
  6. "strings"
  7. )
  8. const MaxPathLength = 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. // Path is an opaque reference to a model.
  28. //
  29. // It is comparable and can be used as a map key.
  30. //
  31. // Users or Path must check Valid before using it.
  32. type Path 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 Path) 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 Path) NameAndTag() string {
  51. r.domain = ""
  52. r.namespace = ""
  53. r.build = ""
  54. return r.String()
  55. }
  56. func (r Path) NameTagAndBuild() string {
  57. r.domain = ""
  58. r.namespace = ""
  59. return r.String()
  60. }
  61. // String returns the fully qualified ref string.
  62. func (r Path) 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 Path) 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 Path) 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 Path) Less(o Path) 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 Path) 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 Path) Domain() string { return r.namespace }
  118. func (r Path) Namespace() string { return r.namespace }
  119. func (r Path) Name() string { return r.name }
  120. func (r Path) Tag() string { return r.tag }
  121. func (r Path) Build() string { return r.build }
  122. // ParsePath parses a model path string into a Path.
  123. //
  124. // Examples of valid paths:
  125. //
  126. // "example.com/mistral:7b+x"
  127. // "example.com/mistral:7b+Q4_0"
  128. // "mistral:7b+x"
  129. // "example.com/x/mistral:latest+Q4_0"
  130. // "example.com/x/mistral:latest"
  131. //
  132. // Examples of invalid paths:
  133. //
  134. // "example.com/mistral:7b+"
  135. // "example.com/mistral:7b+Q4_0+"
  136. // "x/y/z/z:8n+I"
  137. // ""
  138. func ParsePath(s string) Path {
  139. var r Path
  140. for kind, part := range Parts(s) {
  141. switch kind {
  142. case Domain:
  143. r.domain = part
  144. case Namespace:
  145. r.namespace = part
  146. case Name:
  147. r.name = part
  148. case Tag:
  149. r.tag = part
  150. case Build:
  151. r.build = strings.ToUpper(part)
  152. case Invalid:
  153. return Path{}
  154. }
  155. }
  156. if !r.Valid() {
  157. return Path{}
  158. }
  159. return r
  160. }
  161. // Merge folds the domain, namespace, tag, and build of b into a if not set.
  162. // The name is left untouched.
  163. //
  164. // Use this for merging a ref with a default ref.
  165. func Merge(a, b Path) Path {
  166. return Path{
  167. // name is left untouched
  168. name: a.name,
  169. domain: cmp.Or(a.domain, b.domain),
  170. namespace: cmp.Or(a.namespace, b.namespace),
  171. tag: cmp.Or(a.tag, b.tag),
  172. build: cmp.Or(a.build, b.build),
  173. }
  174. }
  175. // Parts returns a sequence of the parts of a ref string from most specific
  176. // to least specific.
  177. //
  178. // It normalizes the input string by removing "http://" and "https://" only.
  179. // No other normalization is done.
  180. func Parts(s string) iter.Seq2[PartKind, string] {
  181. return func(yield func(PartKind, string) bool) {
  182. if strings.HasPrefix(s, "http://") {
  183. s = s[len("http://"):]
  184. }
  185. if strings.HasPrefix(s, "https://") {
  186. s = s[len("https://"):]
  187. }
  188. if len(s) > MaxPathLength || len(s) == 0 {
  189. return
  190. }
  191. yieldValid := func(kind PartKind, part string) bool {
  192. if !isValidPart(part) {
  193. yield(Invalid, "")
  194. return false
  195. }
  196. return yield(kind, part)
  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. if !yieldValid(Build, s[i+1:j]) {
  205. return
  206. }
  207. state, j = Tag, i
  208. default:
  209. yield(Invalid, "")
  210. return
  211. }
  212. case ':':
  213. switch state {
  214. case Build, Tag:
  215. if !yieldValid(Tag, s[i+1:j]) {
  216. return
  217. }
  218. state, j = Name, i
  219. default:
  220. yield(Invalid, "")
  221. return
  222. }
  223. case '/':
  224. switch state {
  225. case Name, Tag, Build:
  226. if !yieldValid(Name, s[i+1:j]) {
  227. return
  228. }
  229. state, j = Namespace, i
  230. case Namespace:
  231. if !yieldValid(Namespace, s[i+1:j]) {
  232. return
  233. }
  234. state, j = Domain, i
  235. default:
  236. yield(Invalid, "")
  237. return
  238. }
  239. default:
  240. if !isValidPart(s[i : i+1]) {
  241. yield(Invalid, "")
  242. return
  243. }
  244. }
  245. }
  246. if state <= Namespace {
  247. yieldValid(state, s[:j])
  248. } else {
  249. yieldValid(Name, s[:j])
  250. }
  251. }
  252. }
  253. // Valid returns true if the ref has a valid name. To know if a ref is
  254. // "complete", use Complete.
  255. func (r Path) Valid() bool {
  256. // Parts ensures we only have valid parts, so no need to validate
  257. // them here, only check if we have a name or not.
  258. return r.name != ""
  259. }
  260. // isValidPart returns true if given part is valid ascii [a-zA-Z0-9_\.-]
  261. func isValidPart(s string) bool {
  262. if s == "" {
  263. return false
  264. }
  265. for _, c := range []byte(s) {
  266. if c == '.' || c == '-' {
  267. return true
  268. }
  269. if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_' {
  270. continue
  271. } else {
  272. return false
  273. }
  274. }
  275. return true
  276. }