texttemplate

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// WidecardTag means accepting any none empty string
	// if chose "{}" to accept any none empty string, then should provide another tag value at that level
	WidecardTag = "{}"

	// GJSONTag is the special hardcode tag for indicating GJSON syntax, must appear in the last
	// of one template, if chose "{GJSON}", should provide another tag value at that level
	GJSONTag = "{gjson}"

	DefaultBeginToken = "[["
	DefaultEndToken   = "]]"
	DefaultSeparator  = "."
)

The complete format of template sentence is ${beginToken}${tag1}${separator}${tag2}${separator}...${endtoken} e.g., if beginToken is '[[', endtoken is ']]', separator is '.' [[filter.{}.req.body.{gjson}]] [[filter.{}.req.body.{gjson}]] TemplateEngine is the abstract implementer Template is the part of user's input string's which we want the TemplateEngine to render it MetaTemplate is the description collections for TemplateEngine to identify user's valid template rules

Variables

This section is empty.

Functions

This section is empty.

Types

type DummyTemplate

type DummyTemplate struct{}

DummyTemplate return a empty implement

func (DummyTemplate) ExtractRawTemplateRuleMap

func (DummyTemplate) ExtractRawTemplateRuleMap(input string) map[string]string

ExtractRawTemplateRuleMap dummy implement

func (DummyTemplate) ExtractTemplateRuleMap

func (DummyTemplate) ExtractTemplateRuleMap(input string) map[string]string

ExtractTemplateRuleMap dummy implement

func (DummyTemplate) GetDict

func (DummyTemplate) GetDict() map[string]interface{}

GetDict the dummy implement

func (DummyTemplate) HasTemplates

func (DummyTemplate) HasTemplates(input string) bool

HasTemplates the dummy implement

func (DummyTemplate) MatchMetaTemplate

func (DummyTemplate) MatchMetaTemplate(template string) string

MatchMetaTemplate dummy implement

func (DummyTemplate) Render

func (DummyTemplate) Render(input string) (string, error)

Render dummy implement

func (DummyTemplate) SetDict

func (DummyTemplate) SetDict(template string, value interface{}) error

SetDict the dummy implement

type TemplateEngine

type TemplateEngine interface {
	// Render Rendering e.g., [[xxx.xx.dd.xx]]'s value is 'value0', [[yyy.www.zzz]]'s value is 'value1'
	// "aaa-[[xxx.xx.dd.xx]]-bbb 10101-[[yyy.wwww.zzz]]-9292" will be rendered to "aaa-value0-bbb 10101-value1-9292"
	// Also support GJSON syntax at last tag
	Render(input string) (string, error)

	// ExtractTemplateRuleMap extracts templates from input string
	// return map's key is the template, the value is the matched and rendered metaTemplate
	ExtractTemplateRuleMap(input string) map[string]string

	// ExtractRawTemplateRuleMap extracts templates from input string
	// return map's key is the template, the value is the matched and rendered metaTemplate or empty
	ExtractRawTemplateRuleMap(input string) map[string]string

	// HasTemplates checks whether it has templates in input string or not
	HasTemplates(input string) bool

	// MatchMetaTemplate return original template or replace with {gjson} at last tag, "" if not metaTemplate matched
	MatchMetaTemplate(template string) string

	// SetDict adds a temaplateRule and its value for later rendering
	SetDict(template string, value interface{}) error

	// GetDict returns the template's dictionary
	GetDict() map[string]interface{}
}

TemplateEngine is the basic API collection for a template usage

func New

func New(beginToken, endToken, separator string, metaTemplates []string) (TemplateEngine, error)

New returns a new Template interface implementer, return a dummy template if something wrong, and in that case, the dedicated reason will set into error return

func NewDefault

func NewDefault(metaTemplates []string) (TemplateEngine, error)

NewDefault returns Template interface implementer with default config and customize meatTemplates

func NewDummyTemplate

func NewDummyTemplate() TemplateEngine

NewDummyTemplate returns a dummy template implement

type TextTemplate

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

TextTemplate wraps a fasttempalte rendering and a template syntax tree for validation, the valid template and its value can be added into dictionary for rendering

func (TextTemplate) ExtractRawTemplateRuleMap

func (t TextTemplate) ExtractRawTemplateRuleMap(input string) map[string]string

ExtractRawTemplateRuleMap extracts all candidate templates (valid/invalid) from input string

func (TextTemplate) ExtractTemplateRuleMap

func (t TextTemplate) ExtractTemplateRuleMap(input string) map[string]string

ExtractTemplateRuleMap extracts candidate templates from input string return map's key is the candidate template, the value is the matched template

func (TextTemplate) GetDict

func (t TextTemplate) GetDict() map[string]interface{}

GetDict return the dictionary of texttemplate

func (TextTemplate) HasTemplates

func (t TextTemplate) HasTemplates(input string) bool

HasTemplates check a string contain any valid templates

func (TextTemplate) MatchMetaTemplate

func (t TextTemplate) MatchMetaTemplate(template string) string

MatchMetaTemplate travels the metaTemplate syntax tree and return the first match template if matched found

e.g. template is "filter.abc.req.body.friends.#(last=="Murphy").first" match "filter.{}.req.body.{gjson}"
	will return "filter.abc.req.body.{gjson}"
e.g. template is "filter.abc.req.body" match "filter.{}.req.body"
	will return "filter.abc.req.body"

if not any template matched found, then return ""

func (TextTemplate) Render

func (t TextTemplate) Render(input string) (string, error)

Render uses a fasttemplate and dictionary to rendering

e.g., [[xxx.xx.dd.xx]]'s value in dictionary is 'value0', [[yyy.www.zzz]]'s value is 'value1'

"aaa-[[xxx.xx.dd.xx]]-bbb 10101-[[yyy.wwww.zzz]]-9292" will be rendered to "aaa-value0-bbb 10101-value1-9292" if containers any new GJSON syntax, it will use 'gjson.Get' to extract result then store into dictionary before rendering

func (TextTemplate) SetDict

func (t TextTemplate) SetDict(template string, value interface{}) error

SetDict adds a templateRule into dictionary if it contains any templates.

Jump to

Keyboard shortcuts

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