with regex

This commit is contained in:
Kasper Juul Hermansen 2022-11-02 01:56:57 +01:00
parent d484d44981
commit dd225fc130
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
3 changed files with 130 additions and 0 deletions

6
pkg/schema/schema.go Normal file
View File

@ -0,0 +1,6 @@
package schema
type CharSchema struct {
Registry string `json:"registry" yaml:"registry"`
Plugins CharSchemaPlugins `json:"plugins" yaml:"plugins"`
}

View File

@ -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<org>[\d\w\-_\.]+)\/(?P<repo>[\d\w\-_\.]+)(?P<path>#[\d\w\-_\.\/]+)?(?P<version>@[\d\w\-_\.\/]+)?(?P<path>#[\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 {
}

View File

@ -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)
})
}
}