73 lines
1.6 KiB
Go
73 lines
1.6 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.(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()
|
|
}
|
|
|
|
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()
|
|
}
|