Combine loaderImpl and fileLoader.

This commit is contained in:
Jeffrey Regan
2018-07-25 17:23:04 -07:00
parent 4569a09d54
commit 9e5374e725
8 changed files with 57 additions and 85 deletions

View File

@@ -17,8 +17,6 @@ limitations under the License.
// Package loader has a data loading interface and various implementations.
package loader
import "fmt"
// Loader interface exposes methods to read bytes.
type Loader interface {
// Root returns the root location for this Loader.
@@ -30,60 +28,3 @@ type Loader interface {
// GlobLoad returns the bytes read from a glob path or an error.
GlobLoad(location string) (map[string][]byte, error)
}
// Private implementation of Loader interface.
type loaderImpl struct {
root string
fLoader *FileLoader
}
const emptyRoot = ""
// NewLoader initializes the first loader with the supported fLoader.
func NewLoader(fl *FileLoader) Loader {
return &loaderImpl{root: emptyRoot, fLoader: fl}
}
// Root returns the root location for this Loader.
func (l *loaderImpl) Root() string {
return l.root
}
// Returns a new Loader rooted at newRoot. "newRoot" MUST be
// a directory (not a file). The directory can have a trailing
// slash or not.
// Example: "/home/seans/project" or "/home/seans/project/"
// NOT "/home/seans/project/file.yaml".
func (l *loaderImpl) New(newRoot string) (Loader, error) {
if !l.fLoader.IsAbsPath(l.root, newRoot) {
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, newRoot)
}
root, err := l.fLoader.FullLocation(l.root, newRoot)
if err != nil {
return nil, err
}
return &loaderImpl{root: root, fLoader: l.fLoader}, nil
}
// Load returns all the bytes read from location or an error.
// "location" can be an absolute path, or if relative, full location is
// calculated from the Root().
func (l *loaderImpl) Load(location string) ([]byte, error) {
fullLocation, err := l.fLoader.FullLocation(l.root, location)
if err != nil {
fmt.Printf("Trouble in fulllocation: %v\n", err)
return nil, err
}
return l.fLoader.Load(fullLocation)
}
// GlobLoad returns a map from path to bytes read from the location or an error.
// "location" can be an absolute path, or if relative, full location is
// calculated from the Root().
func (l *loaderImpl) GlobLoad(location string) (map[string][]byte, error) {
fullLocation, err := l.fLoader.FullLocation(l.root, location)
if err != nil {
return nil, err
}
return l.fLoader.GlobLoad(fullLocation)
}