template

package
v0.0.0-...-f83813f Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: Apache-2.0 Imports: 30 Imported by: 1

Documentation

Index

Constants

View Source
const MaxTemplateSize = 1024 * 1024 * 1

MaxTemplateSize is 1MiB.

View Source
const TranslationJSONName = "translation.json"

Variables

View Source
var ErrLimitReached = errors.New("template: rendered template is too large")
View Source
var ErrMissingFeatureFlagInCtx = errors.New("template: feature flag config missing in ctx")
View Source
var ErrNotFound = errors.New("requested template not found")
View Source
var ErrUpdateDisallowed = errors.New("template: update disallowed")
View Source
var TranslationJSON = resource.RegisterResource(&translationJSON{})

Functions

func Embed

func Embed(data map[string]interface{}, value interface{})

Embed embeds the given value into data.

func EnsureTime

func EnsureTime(anyValue interface{}) interface{}

func HTMLAttr

func HTMLAttr(v string) template.HTMLAttr

func IsNil

func IsNil(v interface{}) bool

func MakeTemplateFuncMap

func MakeTemplateFuncMap(t tpl) map[string]interface{}

func RFC3339

func RFC3339(date interface{}) interface{}

func ResolveError

func ResolveError(apiErr *apierrors.APIError, input ResolveErrorRawInput) map[string]interface{}

func ShowAttributeValue

func ShowAttributeValue(v interface{}) string

Types

type DefaultLanguageTag

type DefaultLanguageTag string

type Engine

type Engine struct {
	Resolver EngineTemplateResolver
}

func (*Engine) Render

func (e *Engine) Render(resource Resource, preferredLanguages []string, data interface{}) (*RenderResult, error)

func (*Engine) RenderStatus

func (e *Engine) RenderStatus(w http.ResponseWriter, r *http.Request, status int, tpl Resource, data interface{})

func (*Engine) Translation

func (e *Engine) Translation(preferredLanguages []string) (*TranslationMap, error)

type EngineTemplateResolver

type EngineTemplateResolver interface {
	ResolveHTML(desc *HTML, preferredLanguages []string) (*HTMLTemplateEffectiveResource, error)
	ResolveMessageHTML(desc *MessageHTML, preferredLanguages []string) (*HTMLTemplateEffectiveResource, error)
	ResolvePlainText(desc *PlainText, preferredLanguages []string) (*TextTemplateEffectiveResource, error)
	ResolveMessagePlainText(desc *MessagePlainText, preferredLanguages []string) (*TextTemplateEffectiveResource, error)
	ResolveTranslations(preferredLanguages []string) (map[string]Translation, error)
}

type GetErrorInput

type GetErrorInput struct {
	ByReason   []string `json:"by_reason,omitempty"`
	ByLocation []string `json:"by_location,omitempty"` // this field actually check both location and required field
}

type HTML

type HTML struct {
	// Name is the name of template
	Name string
	// ComponentDependencies is the HTML component templates this template depends on.
	ComponentDependencies []*HTML
}

HTML defines a HTML template that is non-customizable

func RegisterHTML

func RegisterHTML(name string, dependencies ...*HTML) *HTML

func (*HTML) FindResources

func (t *HTML) FindResources(fs resource.Fs) ([]resource.Location, error)

func (*HTML) MatchResource

func (t *HTML) MatchResource(path string) (*resource.Match, bool)

func (*HTML) UpdateResource

func (t *HTML) UpdateResource(_ context.Context, _ []resource.ResourceFile, resrc *resource.ResourceFile, data []byte) (*resource.ResourceFile, error)

func (*HTML) ViewResources

func (t *HTML) ViewResources(resources []resource.ResourceFile, view resource.View) (interface{}, error)

type HTMLTemplateEffectiveResource

type HTMLTemplateEffectiveResource struct {
	Data        []byte
	LanguageTag string
	Template    *htmltemplate.Template
}

type LimitWriter

type LimitWriter struct {
	// Writer is the underlying writer.
	Writer io.Writer
	// RemainingQuota is the remaining quota in bytes.
	RemainingQuota int64
}

func NewLimitWriter

func NewLimitWriter(w io.Writer) *LimitWriter

func (*LimitWriter) Write

func (w *LimitWriter) Write(p []byte) (n int, err error)

type MessageHTML

type MessageHTML struct {
	// Name is the name of template
	Name string
}

MessageHTML defines a HTML template that is customizable

func RegisterMessageHTML

func RegisterMessageHTML(name string) *MessageHTML

func (*MessageHTML) FindResources

func (t *MessageHTML) FindResources(fs resource.Fs) ([]resource.Location, error)

func (*MessageHTML) MatchResource

func (t *MessageHTML) MatchResource(path string) (*resource.Match, bool)

func (*MessageHTML) UpdateResource

func (t *MessageHTML) UpdateResource(ctx context.Context, _ []resource.ResourceFile, resrc *resource.ResourceFile, data []byte) (*resource.ResourceFile, error)

func (*MessageHTML) ViewResources

func (t *MessageHTML) ViewResources(resources []resource.ResourceFile, view resource.View) (interface{}, error)

type MessagePlainText

type MessagePlainText struct {
	// Name is the name of template
	Name string
}

MessagePlainText defines a plain text template that is customizable

func RegisterMessagePlainText

func RegisterMessagePlainText(name string) *MessagePlainText

func (*MessagePlainText) FindResources

func (t *MessagePlainText) FindResources(fs resource.Fs) ([]resource.Location, error)

func (*MessagePlainText) MatchResource

func (t *MessagePlainText) MatchResource(path string) (*resource.Match, bool)

func (*MessagePlainText) UpdateResource

func (t *MessagePlainText) UpdateResource(ctx context.Context, _ []resource.ResourceFile, resrc *resource.ResourceFile, data []byte) (*resource.ResourceFile, error)

func (*MessagePlainText) ViewResources

func (t *MessagePlainText) ViewResources(resources []resource.ResourceFile, view resource.View) (interface{}, error)

type PlainText

type PlainText struct {
	// Name is the name of template
	Name string
	// ComponentDependencies is the plain text component templates this template depends on.
	ComponentDependencies []*PlainText
}

PlainText defines a plain text template that is non-customizable

func RegisterPlainText

func RegisterPlainText(name string, dependencies ...*PlainText) *PlainText

func (*PlainText) FindResources

func (t *PlainText) FindResources(fs resource.Fs) ([]resource.Location, error)

func (*PlainText) MatchResource

func (t *PlainText) MatchResource(path string) (*resource.Match, bool)

func (*PlainText) UpdateResource

func (t *PlainText) UpdateResource(_ context.Context, _ []resource.ResourceFile, resrc *resource.ResourceFile, data []byte) (*resource.ResourceFile, error)

func (*PlainText) ViewResources

func (t *PlainText) ViewResources(resources []resource.ResourceFile, view resource.View) (interface{}, error)

type RenderResult

type RenderResult struct {
	String      string
	LanguageTag string
}

type ResolveErrorInput

type ResolveErrorInput map[string]*GetErrorInput

type ResolveErrorRawInput

type ResolveErrorRawInput map[string]interface{} // actually map[string][]string, but go template dict always map[string]interface{}

type Resolver

type Resolver struct {
	Resources             ResourceManager
	DefaultLanguageTag    DefaultLanguageTag
	SupportedLanguageTags SupportedLanguageTags
}

func (*Resolver) ResolveHTML

func (r *Resolver) ResolveHTML(desc *HTML, preferredLanguages []string) (*HTMLTemplateEffectiveResource, error)

func (*Resolver) ResolveMessageHTML

func (r *Resolver) ResolveMessageHTML(desc *MessageHTML, preferredLanguages []string) (*HTMLTemplateEffectiveResource, error)

func (*Resolver) ResolveMessagePlainText

func (r *Resolver) ResolveMessagePlainText(desc *MessagePlainText, preferredLanguages []string) (*TextTemplateEffectiveResource, error)

func (*Resolver) ResolvePlainText

func (r *Resolver) ResolvePlainText(desc *PlainText, preferredLanguages []string) (*TextTemplateEffectiveResource, error)

func (*Resolver) ResolveTranslations

func (r *Resolver) ResolveTranslations(preferredLanguages []string) (map[string]Translation, error)

type Resource

type Resource interface {
	// contains filtered or unexported methods
}

type ResourceManager

type ResourceManager interface {
	Read(desc resource.Descriptor, view resource.View) (interface{}, error)
}

type SupportedLanguageTags

type SupportedLanguageTags []string

type TextTemplateEffectiveResource

type TextTemplateEffectiveResource struct {
	Data        []byte
	LanguageTag string
	Template    *texttemplate.Template
}

type Translation

type Translation struct {
	LanguageTag string
	Value       string
}

func (Translation) GetLanguageTag

func (t Translation) GetLanguageTag() string

type TranslationMap

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

func (*TranslationMap) HasKey

func (t *TranslationMap) HasKey(key string) bool

func (*TranslationMap) RenderText

func (t *TranslationMap) RenderText(key string, args interface{}) (string, error)

type Validator

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

func NewValidator

func NewValidator(opts ...ValidatorOption) *Validator

func (*Validator) ValidateHTMLTemplate

func (v *Validator) ValidateHTMLTemplate(template *html.Template) error

func (*Validator) ValidateTextTemplate

func (v *Validator) ValidateTextTemplate(template *text.Template) error

type ValidatorOption

type ValidatorOption func(*Validator)

func AllowDeclaration

func AllowDeclaration(b bool) ValidatorOption

func AllowIdentifierNode

func AllowIdentifierNode(b bool) ValidatorOption

func AllowRangeNode

func AllowRangeNode(b bool) ValidatorOption

func AllowTemplateNode

func AllowTemplateNode(b bool) ValidatorOption

func MaxDepth

func MaxDepth(d int) ValidatorOption

Jump to

Keyboard shortcuts

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