persona

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: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDefaultKind

func GetDefaultKind() *state.Kind

Returns a pointer to the default Kind, which has a Name of "persona" and a FolderName of "personas"

Types

type Persona

type Persona[D state.Initializable[D, *D], S state.Initializable[S, *S]] struct {
	// The Name of the Persona; this is used in messaging and to determine the name of the Persona's state file.
	// Every Persona must have a name - if one is not specified, it defaults to "default" when the Persona is
	// initialized; this is used for the name of the state file on disk as "{Name}.yaml".
	//
	// Example Behavior:
	//
	// With Name "foo":
	//     "foo.yaml"
	// With Name "foo_bar.baz":
	//     "foo_bar.baz.yaml"
	Name string
	// The Kind of Persona this is. The Name field is used in log messaging/displays about the Persona while the
	// FolderName field is used to determine where to save it and look for it when loading. If not specified, it
	// defaults to state.Kind{Name: "persona", FolderName: "personas"}
	//
	// Example Behavior:
	//
	// With root folder "myApp", Kind.FolderName "users", and Name "foo":
	//     "myApp/saves/foo.yaml"
	// With root folder "myApp", Kind.FolderName "modes", and Name "interactive":
	//     "myApp/modes/interactive.yaml"
	Kind state.Kind
	// The handler for this Persona's state to enable file discovery and save/load operations.
	Handle *state.Handle
	// Personas have Data of the type you specify when instantiating them. It must be a struct of the information related
	// to this Persona specifically. This can be any struct you want as long as:
	//
	// 1. It has an Initialize() method that returns a pointer to the data structure itself. You want to make sure that
	// this method sets sensible defaults for the data if you need any - otherwise it can just return a pointer to an
	// empty instance of the data type.
	//
	// 2. It can be marshalled/unmarshalled by viper. In general, you don't need to worry about this unless you're doing
	// something very complex. No such errors or issues have been reported, but viper is what is being used further down
	// the stack to load/save the state file for an instance so if viper can't handle it the instance will error.
	Data D
	// Settings is a struct that determins how this Persona should behave in the application. The Settings field is always
	// determined by the application developer and can be any struct so long as it has an Initialize() method which
	// returns a pointer to itself.
	// Personas have Settings of the type you specify when instantiating them and determine how this Persona should behave
	// in the application. The Settings can be any struct you want as long as:
	//
	// 1. It has an Initialize() method that returns a pointer to the settings struct itself. You want to make sure that
	// this method sets sensible defaults for the data if you need any - otherwise it can just return a pointer to an
	// empty instance of the data type.
	//
	// 2. It can be marshalled/unmarshalled by viper. In general, you don't need to worry about this unless you're doing
	// something very complex. No such errors or issues have been reported, but viper is what is being used further down
	// the stack to load/save the state file for an instance so if viper can't handle it the instance will error.
	Settings S
}

A Persona is a generic structure representing some sort of user mode or profile that changes the internal behavior of an application. It must have structured Data and Settings. Personas use viper to save and load their state.

func DiscoverPersonas

func DiscoverPersonas[D state.Initializable[D, *D], S state.Initializable[S, *S]](kind *state.Kind, dataFolderPath string, afs *afero.Afero) (personas []*Persona[D, S], err error)

DiscoverPersonas is a convenience function to look for Personas in a folder and initialize them all. The kind tells the function where to expect to find the personas. Because the Kind is a pointer, you can pass nil if you want; this will default to using the default "persona" kind, which is saved into a "personas" subfolder. The specified root folder path must be where you expect to find the subfolder specified by the kind parameter; this subfolder should contain the Persona state files.

DiscoverPersonas will stop processing as soon as an error is encountered, whether that's during the initial discovery of the Personas or the initialization of one of them; currently there is no handling to ignore a malformed state file for a Persona and load the others.

There is currently no auto-handling of instances associated with a Persona; each Tympan application using the Persona-associated instance model will need to implement their own handling.

Examples:

When looking in the user data folder for your app:

kind := &persona.Kind{Name: "user", FolderName: "users"}
persona.DiscoverPersonas[MyUserData, MyUserSettings](kind, "~/.myApp/data", afs)

The function will look in "~/.myApp/data/users" for yaml files and attempt to initialize them as Personas where the Data field is of the `MyUserData` type and the Settings field is of the `MyUserSettings` type.

persona.DiscoverPersonas[MyUserData, MyUserSettings](nil, "~/.myApp/data", afs)

The function will look in "~/.myApp/data/personas" for yaml files and attempt to initialize them as Personas where the Data field is of the `MyUserData` type and the Settings field is of the `MyUserSettings` type.

func GetPersona

func GetPersona[D state.Initializable[D, *D], S state.Initializable[S, *S]](name string, kind *state.Kind, rootFolderPath string, afs *afero.Afero) (persona *Persona[D, S], err error)

GetPersona is a convenience function to find, load, and return a single Persona. You must pass the name of the Persona you're looking for, the Kind of Persona you're looking for, the root folder path to where you expect to find Personas, and an Afero file system. If you specify the Kind as nil, GetPersona will use the default Kind. GetPersona creates a Persona object with the specified Name and Kind and then initializes the handle to that Persona before attempting to Load it, returning the Persona with values populated from disk. If the load step fails, it returns a zero representation of the Persona and the error.

func (*Persona[D, S]) FolderPath

func (persona *Persona[D, S]) FolderPath(rootFolderPath string) string

FolderPath takes the root folder where Personas are expected to be found and appends the file-path-safe foldername as specified by this Persona's Kind; if no Kind is specified, the default Kind is used.

func (*Persona[D, S]) Initialize

func (persona *Persona[D, S]) Initialize(name string, rootFolderPath string, afs *afero.Afero) error

Initialize handles the first load/set for a Persona. If the Persona's handle already has a viper, Initialize returns immediately. If the Persona does not have a viper, one is initialized. If the Persona's file already exists, Initialize will try to load it and return. If the file does not exist, Initialize will initialize the Persona's Settings and Data and then write the defaults to disk.

func (*Persona[D, S]) Load

func (persona *Persona[D, S]) Load(afs *afero.Afero) error

Load attempts to read a Persona's Settings and Data from its saved state, updating the Persona if able/needed. It expects that the Persona's state Handle has already been initialized. If successful, it updates the Handle's FileInfo. If the operation fails, it returns an error.

func (*Persona[D, S]) Save

func (persona *Persona[D, S]) Save(afs *afero.Afero) error

Save attempts to write the current in-memory representation of a Persona's Settings and Data to its file, overwriting any conflicting values. It expects that the Persona's state Handle has already been initialized. If successful, it updates the Handle's FileInfo. If unsuccessful, it returns an error.

Jump to

Keyboard shortcuts

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