jx/internal/folio/resourcereference.go

73 lines
1.6 KiB
Go
Raw Permalink Normal View History

2024-08-18 01:19:56 +00:00
// 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 {
2024-09-19 07:57:26 +00:00
if string(r) == "" {
return nil
}
2024-08-18 01:19:56 +00:00
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().URL()
2024-08-18 01:19:56 +00:00
}
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()
}