messagen

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2019 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

messagen is the tree structured template engine with declarative API.

Example
package main

import (
	"fmt"
	"math/rand"

	"github.com/mpppk/messagen/messagen"
)

func main() {
	generator, _ := messagen.New(nil)

	definitions := []*messagen.Definition{
		{
			// Each Definition have one Type, and multiple definitions can have the same Type.
			// Definitions are referred from variable in templates via Type.
			// If referred Type is shared by multiple definitions, they are chosen randomly.
			Type: "Root",

			// Templates are template for generate message.
			// If two ore more templates are given, one of them is picked at random.
			// You can write template like golang text/template format. (but currently functions are unavailable)
			// If Type is embedded by the notation like {{.SomeType}},
			// one of definition that have the Type is chosen and inject generated message.
			// For example, below template refers three Types, Pronoun, FirstName, and LastName.
			Templates: []string{"{{.Pronoun}} is {{.FirstName}} {{.LastName}}."},
		},
		{
			Type:      "Pronoun",
			Templates: []string{"She"},

			// Definition can have Constraints, which key value map for control whether the definition is picked.
			// Constraints key is consisted by Type and Operators.
			// Below Constraints key is "Gender+", which consisted by Type(Gender) and Operator(+).
			// Operator add more constraints related to the key.
			// For example, below "+" operator means...
			// * Gender key is optional. So even if Gender key does not exist, this definitions can be picked.
			//   * If so, Gender key whose value is Female is added.
			// * If Gender key does exist and value is not Female, the definition can not be picked.
			Constraints: map[string]string{"Gender+": "Female"},
		},
		{
			Type:        "Pronoun",
			Templates:   []string{"He"},
			Constraints: map[string]string{"Gender+": "Male"},
		},
		{
			Type:        "FirstName",
			Templates:   []string{"Liam", "James", "Benjamin"},
			Constraints: map[string]string{"Gender+": "Male"},
		},
		{
			Type:        "FirstName",
			Templates:   []string{"Emily", "Charlotte", "Sofia"},
			Constraints: map[string]string{"Gender+": "Female"},
		},
		{
			Type:      "LastName",
			Templates: []string{"Smith", "Williams", "Brown"},
		},
	}

	// AddDefinition definitions to generator.
	_ = generator.AddDefinition(definitions...)

	// Set random seed for pick definitions and templates.
	rand.Seed(0)

	// Generate method generate message according to added definitions.
	// First argument represent definition Type of start point.
	messages, _ := generator.Generate("Root", nil, 1)

	// Second argument represent initial state.
	// In below code, Gender key is added with Female value as initial state.
	// Therefore, Pronoun and FirstName definitions that have constraints which include Gender:Female are always picked.
	femaleMessages, _ := generator.Generate("Root", map[string]string{"Gender": "Female"}, 1)

	maleMessages, _ := generator.Generate("Root", map[string]string{"Gender": "Male"}, 1)
	fmt.Printf("%s\n%s\n%s\n", messages[0], femaleMessages[0], maleMessages[0])

	// Output
	// She is Charlotte Williams.
	// She is Emily Smith.
	// He is James Smith.
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var RandomTemplatePicker = internal.RandomTemplatePicker

Functions

This section is empty.

Types

type Alias

type Alias struct {
	Type           string `yaml:"Type"`
	AllowDuplicate bool   `yaml:"AllowDuplicate"`
}

type Config

type Config struct {
	Definitions []*Definition `yaml:"Definitions"`
}

func ParseYaml

func ParseYaml(contents []byte) (*Config, error)
Example
package main

import (
	"fmt"

	"github.com/mpppk/messagen/messagen"
)

func main() {
	contents := `
Definitions:
  - Type: Root
    Templates: ["hello"]
`

	config, _ := messagen.ParseYaml([]byte(contents))
	def := config.Definitions[0]
	fmt.Println(def.Type, def.Templates[0])

}
Output:

Root hello

func ParseYamlFile

func ParseYamlFile(filePath string) (*Config, error)

type Definition

type Definition struct {
	Type           string            `yaml:"Type"`
	Templates      []string          `yaml:"Templates"`
	Constraints    map[string]string `yaml:"Constraints"`
	Aliases        map[string]*Alias `yaml:"Aliases"`
	AllowDuplicate bool              `yaml:"AllowDuplicate"`
	Order          []string          `yaml:"Order"`
	Weight         float32           `yaml:"Weight"`
}

type DefinitionPicker

type DefinitionPicker = internal.DefinitionPicker

type DefinitionWithAlias

type DefinitionWithAlias = internal.DefinitionWithAlias

type Messagen

type Messagen struct {
	// contains filtered or unexported fields
}

func New

func New(opt *Option) (*Messagen, error)

func (*Messagen) AddDefinition

func (m *Messagen) AddDefinition(defs ...*Definition) error

func (*Messagen) Generate

func (m *Messagen) Generate(defType string, state map[string]string, num uint) ([]string, error)

type Option

type Option struct {
	TemplatePickers    []internal.TemplatePicker
	DefinitionPickers  []internal.DefinitionPicker
	TemplateValidators []internal.TemplateValidator
}

type State

type State = internal.State

type Template

type Template = internal.Template

type TemplatePicker

type TemplatePicker = internal.TemplatePicker

type TemplateValidator

type TemplateValidator = internal.TemplateValidator

type Templates

type Templates = internal.Templates

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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