jx/internal/types/types.go

132 lines
3.4 KiB
Go
Raw Normal View History

2024-05-31 16:11:35 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package types
import (
"errors"
"fmt"
"net/url"
"strings"
"path/filepath"
2024-09-25 05:13:53 +00:00
"log/slog"
"runtime/debug"
2024-05-31 16:11:35 +00:00
)
2024-07-01 07:16:55 +00:00
/*
The `types` package provides a generic method of registering a type factory.
*/
2024-05-31 16:11:35 +00:00
var (
ErrUnknownType = errors.New("Unknown type")
ErrInvalidProduct = errors.New("Invalid product")
2024-05-31 16:11:35 +00:00
)
type Factory[Product comparable] func(*url.URL) Product
type RegistryTypeMap[Product comparable] map[string]Factory[Product]
2024-05-31 16:11:35 +00:00
2024-09-25 05:13:53 +00:00
//type Name[Registry any] string //`json:"type"`
type Types[Product comparable] struct {
2024-05-31 16:11:35 +00:00
registry RegistryTypeMap[Product]
2024-09-25 05:13:53 +00:00
contentTypes RegistryTypeMap[Product]
2024-05-31 16:11:35 +00:00
}
func New[Product comparable]() *Types[Product] {
2024-09-25 05:13:53 +00:00
return &Types[Product]{registry: make(map[string]Factory[Product]), contentTypes: make(map[string]Factory[Product])}
2024-05-31 16:11:35 +00:00
}
func (t *Types[Product]) Register(names []string, factory Factory[Product]) {
for _,name := range names {
t.registry[name] = factory
}
}
2024-09-25 05:13:53 +00:00
func (t *Types[Product]) RegisterContentType(contenttypes []string, factory Factory[Product]) {
for _,name := range contenttypes {
t.contentTypes[name] = factory
}
}
2024-05-31 16:11:35 +00:00
func (t *Types[Product]) FromExtension(path string) (Factory[Product], error) {
elements := strings.Split(path, ".")
numberOfElements := len(elements)
2024-09-25 05:13:53 +00:00
slog.Info("Types[Product].FromExtension()", "path", path, "elements", elements, "types", t.contentTypes, "stacktrace", string(debug.Stack()))
2024-05-31 16:11:35 +00:00
if numberOfElements > 2 {
2024-09-25 05:13:53 +00:00
ext := strings.Join(elements[numberOfElements - 2: numberOfElements], ".")
slog.Info("Types[Product].FromExtension() - Lookup", "ext", ext, "stacktrace", string(debug.Stack()))
if src := t.GetContentType(ext); src != nil {
2024-05-31 16:11:35 +00:00
return src, nil
}
}
2024-09-25 05:13:53 +00:00
if src := t.GetContentType(elements[numberOfElements - 1]); src != nil {
2024-05-31 16:11:35 +00:00
return src, nil
}
return nil, fmt.Errorf("%w: %s", ErrUnknownType, path)
}
func (t *Types[Product]) New(uri string) (result Product, err error) {
u, e := url.Parse(uri)
if u == nil || e != nil {
2024-09-25 05:13:53 +00:00
err = fmt.Errorf("%w: %s %s", ErrUnknownType, e, uri)
2024-05-31 16:11:35 +00:00
return
}
2024-09-25 05:13:53 +00:00
return t.NewFromParsedURI(u)
}
2024-05-31 16:11:35 +00:00
2024-09-25 05:13:53 +00:00
func (t *Types[Product]) NewFromParsedURI(u *url.URL) (result Product, err error) {
2024-05-31 16:11:35 +00:00
if u.Scheme == "" {
u.Scheme = "file"
}
path := filepath.Join(u.Hostname(), u.Path)
if d, lookupErr := t.FromExtension(path); d != nil {
return d(u), lookupErr
2024-09-25 05:13:53 +00:00
} else {
slog.Info("Types[Product].NewFromParsedURI() - FromExtension()", "uri", u, "path", path, "error", lookupErr, "stacktrace", string(debug.Stack()))
2024-05-31 16:11:35 +00:00
}
if r, ok := t.registry[u.Scheme]; ok {
if result = r(u); result != any(nil) {
return result, nil
} else {
2024-09-25 05:13:53 +00:00
return result, fmt.Errorf("%w: factory failed creating %s", ErrInvalidProduct, u.String())
}
2024-05-31 16:11:35 +00:00
}
err = fmt.Errorf("%w: %s", ErrUnknownType, u.Scheme)
return
}
func (t *Types[Product]) Has(typename string) bool {
if _, ok := t.registry[typename]; ok {
return true
}
return false
}
2024-09-25 05:13:53 +00:00
func (t *Types[Product]) HasContentType(contenttype string) bool {
if _, ok := t.contentTypes[contenttype]; ok {
return true
}
return false
}
func (t *Types[Product]) GetContentType(contenttype string) Factory[Product] {
if d, ok := t.contentTypes[contenttype]; ok {
2024-05-31 16:11:35 +00:00
return d
}
return nil
}
/*
2024-07-01 07:16:55 +00:00
func (n *Name[Registry]) UnmarshalJSON(b []byte) error {
ProductTypeName := strings.Trim(string(b), "\"")
if Registry.Has(ProductTypeName) {
*n = Name[Registry](ProductTypeName)
2024-05-31 16:11:35 +00:00
return nil
}
2024-07-01 07:16:55 +00:00
return fmt.Errorf("%w: %s", ErrUnknownType, ProductTypeName)
2024-05-31 16:11:35 +00:00
}
*/