From dd225fc13073afb3e6775c8ebf57dd74f33c703d Mon Sep 17 00:00:00 2001 From: kjuulh Date: Wed, 2 Nov 2022 01:56:57 +0100 Subject: [PATCH] with regex --- pkg/schema/schema.go | 6 +++ pkg/schema/schema_plugin.go | 52 +++++++++++++++++++++++ pkg/schema/schema_plugin_test.go | 72 ++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 pkg/schema/schema.go create mode 100644 pkg/schema/schema_plugin.go create mode 100644 pkg/schema/schema_plugin_test.go diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go new file mode 100644 index 0000000..f209a8b --- /dev/null +++ b/pkg/schema/schema.go @@ -0,0 +1,6 @@ +package schema + +type CharSchema struct { + Registry string `json:"registry" yaml:"registry"` + Plugins CharSchemaPlugins `json:"plugins" yaml:"plugins"` +} diff --git a/pkg/schema/schema_plugin.go b/pkg/schema/schema_plugin.go new file mode 100644 index 0000000..2b6f421 --- /dev/null +++ b/pkg/schema/schema_plugin.go @@ -0,0 +1,52 @@ +package schema + +import ( + "regexp" + "strings" +) + +type CharSchemaPluginName string + +type PluginOps struct { + Org string + RepositoryName string + Path string + Version string +} + +func (cspn CharSchemaPluginName) Get() *PluginOps { + po := &PluginOps{} + reg := regexp.MustCompile( + `(?P[\d\w\-_\.]+)\/(?P[\d\w\-_\.]+)(?P#[\d\w\-_\.\/]+)?(?P@[\d\w\-_\.\/]+)?(?P#[\d\w\-_\.\/]+)?`, + ) + matches := reg.FindStringSubmatch(string(cspn)) + tags := reg.SubexpNames() + + matchTags := make(map[string]string, len(matches)) + for i, match := range matches { + tag := tags[i] + if existingTag, ok := matchTags[tag]; !ok || existingTag == "" { + matchTags[tag] = match + } + } + + if org, ok := matchTags["org"]; ok { + po.Org = org + } + if repo, ok := matchTags["repo"]; ok { + po.RepositoryName = repo + } + if path, ok := matchTags["path"]; ok { + po.Path = strings.TrimLeft(path, "#") + } + if version, ok := matchTags["version"]; ok { + po.Version = strings.TrimLeft(version, "@") + } + + return po +} + +type CharSchemaPlugins map[CharSchemaPluginName]CharSchemaPlugin + +type CharSchemaPlugin struct { +} diff --git a/pkg/schema/schema_plugin_test.go b/pkg/schema/schema_plugin_test.go new file mode 100644 index 0000000..7db196d --- /dev/null +++ b/pkg/schema/schema_plugin_test.go @@ -0,0 +1,72 @@ +package schema_test + +import ( + "testing" + + "git.front.kjuulh.io/kjuulh/char/pkg/schema" + "github.com/stretchr/testify/require" +) + +func TestSchemaNameCanParse(t *testing.T) { + t.Parallel() + + tt := []struct { + name string + inputString schema.CharSchemaPluginName + expected schema.PluginOps + }{ + { + name: "default string", + inputString: `kju123K_-ulh/someRepo-._123`, + expected: schema.PluginOps{ + Org: "kju123K_-ulh", + RepositoryName: "someRepo-._123", + }, + }, + { + name: "default string with path", + inputString: `kju123K_-ulh/someRepo-._123#somepath/sometoherpath/somethridpath`, + expected: schema.PluginOps{ + Org: "kju123K_-ulh", + RepositoryName: "someRepo-._123", + Path: "somepath/sometoherpath/somethridpath", + }, + }, + { + name: "default string with version", + inputString: `kju123K_-ulh/someRepo-._123@12l3.jk1lj`, + expected: schema.PluginOps{ + Org: "kju123K_-ulh", + RepositoryName: "someRepo-._123", + Version: "12l3.jk1lj", + }, + }, + { + name: "default string with version and path", + inputString: `kju123K_-ulh/someRepo-._123@12l3.jk1lj#somepath/sometoherpath/somethridpath`, + expected: schema.PluginOps{ + Org: "kju123K_-ulh", + RepositoryName: "someRepo-._123", + Version: "12l3.jk1lj", + Path: "somepath/sometoherpath/somethridpath", + }, + }, + { + name: "default string with path and version", + inputString: `kju123K_-ulh/someRepo-._123#somepath/sometoherpath/somethridpath@12l3.jk1lj`, + expected: schema.PluginOps{ + Org: "kju123K_-ulh", + RepositoryName: "someRepo-._123", + Version: "12l3.jk1lj", + Path: "somepath/sometoherpath/somethridpath", + }, + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + actual := tc.inputString.Get() + require.Equal(t, tc.expected, *actual) + }) + } +}