jx/internal/folio/registry.go
Matthew Rich 04b27dc5df
Some checks failed
Lint / golangci-lint (push) Has been cancelled
Declarative Tests / test (push) Has been cancelled
Declarative Tests / build-fedora (push) Has been cancelled
Declarative Tests / build-ubuntu-focal (push) Has been cancelled
add folio package
2024-08-17 18:19:56 -07:00

76 lines
1.9 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package folio
import (
_ "errors"
_ "fmt"
_ "net/url"
_ "strings"
"decl/internal/types"
"decl/internal/data"
"decl/internal/mapper"
"io/fs"
)
var (
)
type Registry struct {
Schemas mapper.Store[URI, fs.FS]
ConfigurationTypes *types.Types[data.Configuration] // Config Factory
ResourceTypes *types.Types[data.Resource] // Resource Factory
ConverterTypes *types.Types[data.Converter] // Converter Factory
Documents []*Document
UriMap mapper.Store[URI, *Document]
DeclarationMap mapper.Store[*Declaration, *Document]
ConfigurationMap mapper.Store[*Block, *Document]
DefaultSchema URI
}
func NewRegistry() *Registry {
r := &Registry{
ConfigurationTypes: types.New[data.Configuration](),
ResourceTypes: types.New[data.Resource](),
ConverterTypes: types.New[data.Converter](),
Documents: make([]*Document, 0, 10),
UriMap: mapper.New[URI, *Document](),
DeclarationMap: mapper.New[*Declaration, *Document](),
ConfigurationMap: mapper.New[*Block, *Document](),
Schemas: mapper.New[URI, fs.FS](),
DefaultSchema: schemaFilesUri,
}
r.Schemas[schemaFilesUri] = schemaFiles
return r
}
func (r *Registry) Get(key *Declaration) (*Document, bool) {
return r.DeclarationMap.Get(key)
}
func (r *Registry) Has(key *Declaration) (bool) {
return r.DeclarationMap.Has(key)
}
func (r *Registry) NewDocument(uri URI) (doc *Document) {
doc = NewDocument(r)
r.Documents = append(r.Documents, doc)
if uri != "" {
r.UriMap[uri] = doc
}
return
}
func (r *Registry) Load(uri URI) (documents []data.Document, err error) {
var extractor data.Converter
var sourceResource data.Resource
if extractor, err = r.ConverterTypes.New(string(uri)); err == nil {
if sourceResource, err = uri.NewResource(nil); err == nil {
documents, err = extractor.(data.ManyExtractor).ExtractMany(sourceResource, nil)
}
}
return
}