jx/internal/folio/resourcereference.go
Matthew Rich 35899c86a5
Some checks failed
Lint / golangci-lint (push) Failing after 9m51s
Declarative Tests / test (push) Failing after 14s
add openpgp resources
2024-11-10 10:27:31 -08:00

77 lines
1.7 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package folio
import (
"decl/internal/transport"
"decl/internal/data"
"errors"
"log/slog"
"net/url"
)
var (
ErrInvalidResourceURI error = errors.New("Invalid resource URI")
)
type ContentReader interface {
ContentReaderStream() (*transport.Reader, error)
}
type ContentWriter interface {
ContentWriterStream() (*transport.Writer, error)
}
type ContentReadWriter interface {
ContentReader
ContentWriter
}
type ResourceReference URI
// Return a Content ReadWriter for the resource referred to.
func (r ResourceReference) Lookup(look data.ResourceMapper) ContentReadWriter {
if string(r) == "" {
return nil
}
slog.Info("ResourceReference.Lookup()", "resourcereference", r, "resourcemapper", look)
if look != nil {
if v,ok := look.Get(string(r)); ok {
return v.Resource().(ContentReadWriter)
}
}
return r
}
func (r ResourceReference) Dereference(look data.ResourceMapper) data.Resource {
slog.Info("ResourceReference.Dereference()", "resourcereference", r, "resourcemapper", look)
if look != nil {
if v,ok := look.Get(string(r)); ok {
slog.Info("ResourceReference.Dereference()", "resourcereference", r, "result", v)
return v.(*Declaration).Attributes
}
}
return nil
}
func (r ResourceReference) Parse() *url.URL {
return URI(r).Parse().URL()
}
func (r ResourceReference) Exists() bool {
return URI(r).Exists()
}
func (r ResourceReference) ContentReaderStream() (*transport.Reader, error) {
return URI(r).ContentReaderStream()
}
func (r ResourceReference) ContentWriterStream() (*transport.Writer, error) {
return URI(r).ContentWriterStream()
}
func (r ResourceReference) IsEmpty() bool {
return URI(r).IsEmpty()
}