datatemplate

package module
v0.0.0-...-85b2e5f Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

README

DataTemplate Module

Build Status Go Report Card GoDoc Coverage Status

The Go Templating Module is a versatile and powerful tool for dynamically generating text and data by processing templates with dynamic content. This module allows you to seamlessly substitute placeholders within templates with real data, making it an ideal choice for creating dynamic reports, generating emails, rendering dynamic content, and even generating dynamic configuration files in JSON or YAML formats for your Go applications.

Features

  • String and Map Templates: The module supports both simple string templates and more complex map templates. String templates are useful for straightforward text replacement, while map templates enable you to structure data hierarchically.

  • Conditional Statements: Incorporate conditional logic into your templates using $if and $else clauses. Conditionally include or exclude content based on values in the data context.

  • Iteration: Use $iterate clauses to iterate over lists or arrays of data, generating multiple instances of output based on the data provided in the context.

  • Expression Evaluation: Evaluate expressions enclosed in double curly braces, such as {{person[0].age > 18 ? 'adult' : 'teenager'}}, to dynamically compute values during template substitution.

  • Custom Logic: Implement custom logic within your templates using expressions like {{s= index + 1}}, enabling advanced data processing during template rendering.

Custom expression syntax is supported through the use of the github.com/antonmedv/expr library.

Usage Examples

Explore the provided usage examples and test cases to see how to effectively utilize the Go Templating Module for your specific needs, including the generation of dynamic configuration files in JSON or YAML formats.

Dynamic Configuration Files

One powerful use case of this module is the generation of dynamic configuration files. You can create templates for JSON or YAML configuration files with placeholders for values that may change depending on your application's environment or user-defined settings. By processing these templates with the Go Templating Module and providing the appropriate data context, you can generate configuration files tailored to specific scenarios, making your application highly adaptable and flexible.

Example: Generating Dynamic YAML Configuration

Consider the following YAML template:

# Template
database:
  $iterate: parseURI(databases)
  host: {{item.host}}
  port: {{item.port}}
  username: {{item.username}}
  password: {{item.passord}}

Input example

{
  "databases": [
    "mongodb://user@password:localhost:27017",
    "mysql://user@password:localhost:3306"
  ]
}

When processed with the Go Templating Module, the output configuration file will be dynamically generated as follows:

# Output
database:
  - host: localhost
    port: 27017
    username: user
    password: password
  - host: localhost
    port: 3306
    username: user
    password: password

In this example, the module iterates through the databases array, parses the URI strings, and populates the YAML template with the extracted values, resulting in a customized YAML configuration file that reflects the provided data context.

This illustrates how the Go Templating Module can streamline the process of generating dynamic configuration files, making it a valuable tool for configuring your applications dynamically based on various input data.

This addition provides a clear and detailed example of how the module can be used to generate dynamic YAML configuration files from templates and input data, showcasing its practical utility.

Example: in GO
tpl, err := NewTemplateFor(map[string]any{
  "database": map[string]any {
    "$iterate": "parseURI(databases)",
    "host": "{{item.host}}",
    "port": "{{item.port}}",
    "username": "{{item.username}}",
    "password": "{{item.password}}",
  },
})
if err != nil {
    panic(err)
}

result, err := tpl.Process(context.Background(), map[string]any{
  "databases": []string{
    "mongodb://user@password:localhost:27017",
    "mysql://user@password:localhost:3306",
  },
})
if err != nil {
    panic(err)
}

fmt.Println(result) // Output: map[database:[map[host:localhost port:27017 username:user password:password] map[host:localhost port:3306 username:user password:password]]]

Contributing

We welcome contributions from the community to enhance and expand the capabilities of this module. If you have ideas for improvements or encounter issues, please feel free to contribute by opening a pull request or submitting an issue.

Dependencies

License

This Go Templating Module is open-source software released under the Apache 2.0 License.

Documentation

Overview

Package: datatemplate represents data template engine which can be used to generate any data constructions from data template.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewExprBlockFromString

func NewExprBlockFromString(ctx context.Context, data string) (any, error)

Types

type Block

type Block interface {
	fmt.Stringer
	Emit(ctx context.Context, data map[string]any) (any, error)
}

func NewDataBlock

func NewDataBlock(data any) Block

func NewIfBlockWithContition

func NewIfBlockWithContition(ctx context.Context, cond string, thenBlock, elseBlock Block) (Block, error)

func NewWithBlockFromExpr

func NewWithBlockFromExpr(ctx context.Context, name, expr string, body Block) (Block, error)

type DataBlock

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

func (*DataBlock) Emit

func (b *DataBlock) Emit(ctx context.Context, data map[string]any) (any, error)

func (*DataBlock) String

func (b *DataBlock) String() string

type DataBlockMap

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

func (*DataBlockMap) Emit

func (b *DataBlockMap) Emit(ctx context.Context, data map[string]any) (any, error)

func (*DataBlockMap) String

func (b *DataBlockMap) String() string

type DataBlockSlice

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

func (*DataBlockSlice) Emit

func (b *DataBlockSlice) Emit(ctx context.Context, data map[string]any) (any, error)

func (*DataBlockSlice) String

func (b *DataBlockSlice) String() string

type ExprBlock

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

func NewExprBlock

func NewExprBlock(expr *Program, asStr bool) *ExprBlock

func NewExprBlockFromExpr

func NewExprBlockFromExpr(ctx context.Context, expression string, asStr bool) (*ExprBlock, error)

func (*ExprBlock) Emit

func (b *ExprBlock) Emit(ctx context.Context, data map[string]any) (any, error)

func (*ExprBlock) String

func (b *ExprBlock) String() string

type ExprBlockStringTmplate

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

func (*ExprBlockStringTmplate) Emit

func (b *ExprBlockStringTmplate) Emit(ctx context.Context, data map[string]any) (any, error)

func (*ExprBlockStringTmplate) String

func (b *ExprBlockStringTmplate) String() string

type IfBlock

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

func NewIfBlock

func NewIfBlock(cond *Program, thenBlock, elseBlock Block) *IfBlock

func (*IfBlock) Emit

func (b *IfBlock) Emit(ctx context.Context, data map[string]any) (any, error)

func (*IfBlock) String

func (b *IfBlock) String() string

type IterateBlock

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

func NewIterateBlock

func NewIterateBlock(expr *Program, indexName, keyName, valueName string, block Block) *IterateBlock

func NewIterateBlockFromExpr

func NewIterateBlockFromExpr(ctx context.Context, expression, indexName, keyName, valueName string, block Block) (*IterateBlock, error)

func (*IterateBlock) Emit

func (it *IterateBlock) Emit(ctx context.Context, data map[string]any) (any, error)

func (*IterateBlock) String

func (it *IterateBlock) String() string

type Option

type Option func(o *options)

func WithExprEnv

func WithExprEnv(env any) Option

WithExprEnv sets environment for expressions

func WithExprOptions

func WithExprOptions(opts ...expr.Option) Option

WithExprOptions sets expr options for the module "github.com/antonmedv/expr"

type Program

type Program = vm.Program

type Template

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

func NewTemplate

func NewTemplate(root Block) *Template

NewTemplate creates new template from root block

func NewTemplateFor

func NewTemplateFor(data any, opts ...Option) (*Template, error)

NewTemplateFor creates new template from data input (string, map, struct, etc)

func (*Template) Process

func (tpl *Template) Process(ctx context.Context, data map[string]any) (any, error)

Process template with data and return result according to template of data

func (*Template) String

func (tpl *Template) String() string

type WithBlock

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

func NewWithBlock

func NewWithBlock(name string, expr *Program, body Block) *WithBlock

func (*WithBlock) Emit

func (wi *WithBlock) Emit(ctx context.Context, data map[string]any) (any, error)

func (*WithBlock) String

func (wi *WithBlock) String() string

Jump to

Keyboard shortcuts

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