From 8867e744ff3c5a59467f316f0f23c272fe66700a Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 1 May 2024 12:14:53 -0700 Subject: [PATCH 1/3] types/model: fix name for hostport --- types/model/name.go | 6 +++++- types/model/name_test.go | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/types/model/name.go b/types/model/name.go index a0a44703..fd4fbfc4 100644 --- a/types/model/name.go +++ b/types/model/name.go @@ -143,7 +143,11 @@ func ParseNameBare(s string) Name { n.RawDigest = MissingPart } - s, n.Tag, _ = cutPromised(s, ":") + // "/" is an illegal tag character, so we can use it to split the host + if strings.LastIndex(s, ":") > strings.LastIndex(s, "/") { + s, n.Tag, _ = cutPromised(s, ":") + } + s, n.Model, promised = cutPromised(s, "/") if !promised { n.Model = s diff --git a/types/model/name_test.go b/types/model/name_test.go index 75659f0d..2f99c5b1 100644 --- a/types/model/name_test.go +++ b/types/model/name_test.go @@ -34,6 +34,14 @@ func TestParseNameParts(t *testing.T) { Model: "model", }, }, + { + in: "host:12345/namespace/model", + want: Name{ + Host: "host:12345", + Namespace: "namespace", + Model: "model", + }, + }, { in: "namespace/model", want: Name{ From 88775e1ff9d3f9510d9e294c90a6f384df615f5a Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 1 May 2024 12:25:29 -0700 Subject: [PATCH 2/3] strip scheme from name --- types/model/name.go | 8 +++++++- types/model/name_test.go | 22 ++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/types/model/name.go b/types/model/name.go index fd4fbfc4..cb890b3a 100644 --- a/types/model/name.go +++ b/types/model/name.go @@ -153,12 +153,18 @@ func ParseNameBare(s string) Name { n.Model = s return n } + s, n.Namespace, promised = cutPromised(s, "/") if !promised { n.Namespace = s return n } - n.Host = s + + scheme, host, ok := strings.Cut(s, "://") + if ! ok { + host = scheme + } + n.Host = host return n } diff --git a/types/model/name_test.go b/types/model/name_test.go index 2f99c5b1..997513b8 100644 --- a/types/model/name_test.go +++ b/types/model/name_test.go @@ -17,6 +17,15 @@ func TestParseNameParts(t *testing.T) { want Name wantValidDigest bool }{ + { + in: "scheme://host:port/namespace/model:tag", + want: Name{ + Host: "host:port", + Namespace: "namespace", + Model: "model", + Tag: "tag", + }, + }, { in: "host/namespace/model:tag", want: Name{ @@ -26,6 +35,15 @@ func TestParseNameParts(t *testing.T) { Tag: "tag", }, }, + { + in: "host:port/namespace/model:tag", + want: Name{ + Host: "host:port", + Namespace: "namespace", + Model: "model", + Tag: "tag", + }, + }, { in: "host/namespace/model", want: Name{ @@ -35,9 +53,9 @@ func TestParseNameParts(t *testing.T) { }, }, { - in: "host:12345/namespace/model", + in: "host:port/namespace/model", want: Name{ - Host: "host:12345", + Host: "host:port", Namespace: "namespace", Model: "model", }, From 997a45503902160017757cf10bf9a4752cc271eb Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 1 May 2024 12:33:41 -0700 Subject: [PATCH 3/3] want filepath --- types/model/name_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/types/model/name_test.go b/types/model/name_test.go index 997513b8..47263c20 100644 --- a/types/model/name_test.go +++ b/types/model/name_test.go @@ -1,6 +1,7 @@ package model import ( + "path/filepath" "reflect" "runtime" "testing" @@ -15,6 +16,7 @@ func TestParseNameParts(t *testing.T) { cases := []struct { in string want Name + wantFilepath string wantValidDigest bool }{ { @@ -25,6 +27,7 @@ func TestParseNameParts(t *testing.T) { Model: "model", Tag: "tag", }, + wantFilepath: filepath.Join("host:port", "namespace", "model", "tag"), }, { in: "host/namespace/model:tag", @@ -34,6 +37,7 @@ func TestParseNameParts(t *testing.T) { Model: "model", Tag: "tag", }, + wantFilepath: filepath.Join("host", "namespace", "model", "tag"), }, { in: "host:port/namespace/model:tag", @@ -43,6 +47,7 @@ func TestParseNameParts(t *testing.T) { Model: "model", Tag: "tag", }, + wantFilepath: filepath.Join("host:port", "namespace", "model", "tag"), }, { in: "host/namespace/model", @@ -51,6 +56,7 @@ func TestParseNameParts(t *testing.T) { Namespace: "namespace", Model: "model", }, + wantFilepath: filepath.Join("host", "namespace", "model", "latest"), }, { in: "host:port/namespace/model", @@ -59,6 +65,7 @@ func TestParseNameParts(t *testing.T) { Namespace: "namespace", Model: "model", }, + wantFilepath: filepath.Join("host:port", "namespace", "model", "latest"), }, { in: "namespace/model", @@ -66,12 +73,14 @@ func TestParseNameParts(t *testing.T) { Namespace: "namespace", Model: "model", }, + wantFilepath: filepath.Join("registry.ollama.ai", "namespace", "model", "latest"), }, { in: "model", want: Name{ Model: "model", }, + wantFilepath: filepath.Join("registry.ollama.ai", "library", "model", "latest"), }, { in: "h/nn/mm:t", @@ -81,6 +90,7 @@ func TestParseNameParts(t *testing.T) { Model: "mm", Tag: "t", }, + wantFilepath: filepath.Join("h", "nn", "mm", "t"), }, { in: part80 + "/" + part80 + "/" + part80 + ":" + part80, @@ -90,6 +100,7 @@ func TestParseNameParts(t *testing.T) { Model: part80, Tag: part80, }, + wantFilepath: filepath.Join(part80, part80, part80, part80), }, { in: part350 + "/" + part80 + "/" + part80 + ":" + part80, @@ -99,6 +110,7 @@ func TestParseNameParts(t *testing.T) { Model: part80, Tag: part80, }, + wantFilepath: filepath.Join(part350, part80, part80, part80), }, { in: "@digest", @@ -123,6 +135,11 @@ func TestParseNameParts(t *testing.T) { if !reflect.DeepEqual(got, tt.want) { t.Errorf("parseName(%q) = %v; want %v", tt.in, got, tt.want) } + + got = ParseName(tt.in) + if tt.wantFilepath != "" && got.Filepath() != tt.wantFilepath { + t.Errorf("parseName(%q).Filepath() = %q; want %q", tt.in, got.Filepath(), tt.wantFilepath) + } }) } }