module

package
v0.0.0-...-8b058bf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDataByFile

func GetDataByFile[T Cachable[T]](modulePath string, dataTypeName string, afs *afero.Afero) ([]T, error)

GetDataByFile must be told what data type it is looking for and given the path to the module folder, the name of the data type, and an Afero file system to use. It expects that the data is stored in a slice under the "entries" key in a yaml file named the same as the passed data type. It will look for the absolute path to the module folder, determine the name of the yaml file, and then call ReadAndParseData with the passed data type and determined file path, returning the slice of discovered entries. If any step fails, it will return an empty slice of the specified data type and the error.

func GetDataByFolder

func GetDataByFolder[T CachableWithSubtype[T]](modulePath string, dataFolderName string, afs *afero.Afero) ([]T, error)

GetDataByFolder must be told what data type it is looking for and given the path to the module folder, the name of the data folder to look in, and an Afero file system to use. It expects that the data is stored in multiple yaml files whose name is the subtype for all entries in that file. It expects that each file stores the data in a slice under the "entries" key. It will look for the absolute path to the module folder, combine that with the data folder name, and then walk the data folder, calling ReadAndParseData on each yaml file it finds, setting their subtype before returning the combined slice of all discovered entries from every parsed data file. If any step fails, it will return an empty slice of the specified data type and the error.

func GetEmbeddedDataByFile

func GetEmbeddedDataByFile[T Cachable[T]](modulePath string, dataTypeName string, efs *embed.FS) ([]T, error)

GetEmbeddedDataByFile must be told what data type it is looking for and given the path to the module folder, the name of the data type, and an embedded file system to use. It expects that the data is stored in a slice under the "entries" key in a yaml file named the same as the passed data type. It will determine the name of the yaml file and then call ReadAndParseData with the passed data type and determined file path, returning the slice of discovered entries. If any step fails, it will return an empty slice of the specified data type and the error.

func GetEmbeddedDataByFolder

func GetEmbeddedDataByFolder[T CachableWithSubtype[T]](modulePath string, dataFolderName string, efs *embed.FS) ([]T, error)

GetEmbeddedDataByFolder must be told what data type it is looking for and given the path to the module folder, the name of the data folder to look in, and an embedded file system to use. It expects that the data is stored in multiple yaml files whose name is the subtype for all entries in that file. It expects that each file stores the data in a slice under the "entries" key. It will join the module folder path with the data folder name and then walk the data folder, calling ReadAndParseEmbeddedData on each yaml file it finds, setting their subtype before returning the combined slice of all discovered entries from every parsed data file. If any step fails, it will return an empty slice of the specified data type and the error.

func ReadAndParseData

func ReadAndParseData[T Cachable[T]](dataFilePath string, afs *afero.Afero) ([]T, error)

ReadAndParseData must be told what data type it is looking for, given the path to the file to read, and an Afero file system to use. It expects that the data is stored in a slice under the "entries" key in a yaml file. It will look for the absolute path to the file, determine the Source for the data (as its parent folder), use viper to read and unmarshal the data, and then return all entries with their Source set. If any step fails, it will return an empty slice of the specified data type and the error.

You need not call ReadAndParseData directly when interacting with a module; you can instead use the more convenient GetDataByFile function, which only needs the module path, the type name of the data file as it is stored on disk, and an Afero file system.

This function is used by both GetDataByFile and GetDataByFolder.

func ReadAndParseEmbeddedData

func ReadAndParseEmbeddedData[T Cachable[T]](dataFilePath string, efs *embed.FS) ([]T, error)

ReadAndParseEmbeddedData must be told what data type it is looking for, given the path to the file to read, and a pointer to the embedded file system to use. It expects that the data is stored in a slice under the "entries" key in a yaml file. It will look for the absolute path to the file, determine the Source for the data (as its parent folder), use viper to read and unmarshal the data, and then return all entries with their Source set. If any step fails, it will return an empty slice of the specified data type and the error.

You need not call ReadAndParseEmbeddedData directly when interacting with a module; you can instead use the more convenient GetEmbeddedDataByFile function, which only needs the module path, the type name of the data file as it is stored on disk, and an embedded file system.

This function is used by both GetEmbeddedDataByFile and GetEmbeddedDataByFolder.

Types

type Cachable

type Cachable[T any] interface {
	// Cachable data from a module must be able to return itself with its Source (the module it comes from) set. The
	// functions in this module cannot set the Source directly due to limitations in the implementation of generic types
	// in go. If this is addressed in the future, this implementation can be simplified. For more information, see the
	// known limitations section of the go1.18 release notes (no anchor, you'll need to search for "known limitations"):
	// https://tip.golang.org/doc/go1.18
	WithSource(source string) T
}

A Cachable type is any struct which implements the necessary methods for Tympan to find and return their data to the application for further use.

type CachableWithSubtype

type CachableWithSubtype[T any] interface {
	Cachable[T]
	// Cachable data from a module with a subtype must be able to return itself with its Subtype (usually, the name of the
	// file it is stored in) set. The functions in this module cannot set the Subtype directly due to both the ambiguity
	// of the field name for the subtype and limitations in the implementation of generic types in go. If this is
	// addressed in the future, this implementation can be simplified. For more information, see the known limitations
	// section of the go1.18 release notes (no anchor, you'll need to search for "known limitations"):
	// https://tip.golang.org/doc/go1.18
	WithSubtype(subtype string) T
}

Some Module data includes a subtype in addition to a source. These types must implement both the Cachable type constraint and the methods below.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL