format

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2019 License: MIT Imports: 8 Imported by: 2

README

go-format

Build Status

Simple string formatting tool with date arithmetics and format (strftime) support

Installation
go get github.com/sirkon/go-format

or

dep ensure -add github.com/sirkon/go-format
Rationale behind

In analytical systems you need to deal with time intervals. This formatter helps to achieve this right at the configuration file level.

Examples
Positional parameters, you can use ${1}, ${2} to address specific parameter (by number)
res := format.Formatp("$ ${} $1 ${0}", 1, 2)
// res = "1 2 2 1"
Named parameters via map[string]interface{}
res := format.Formatm("${name} $count ${weight|1.2}", format.Values{
	"name": "name",
	"count": 12,
	"weight": 0.79,
})
// res = "name 12 0.79"
Named parameters via type guesses
var s struct {
	A string
	Field int
}
s.A = "str"
s.Field = 12
res := format.Formatg("$A $Field", s)

Substructures are supported. Also, any kind of map with keys being one of

  1. string
  2. any kind of integer, signed or unsigned
  3. fmt.Stringer is supported as well.
type t struct {
	A     string
	Field int
}
var s = t{
	A:     "str",
	Field: 12,
}
var d struct {
	F     t
	Entry float64
}
d.F = s
d.Entry = 0.5
res := format.Formatg("$F.A $F.Field $Entry", d)
// res = "str 12 0.500000"
v := map[int]string{
	1: "bc",
	12: "bd"
}
res := format.Formatg("$1-$12")
// res = "bc-bc"
Use given func(string) string function as a source of values

It is possible to use function like os.Getenv as a source of values. Use format.Formatf function for this:

res := format.Formatf("${HOSTTYPE} ${COLUMNS}", os.Getenv)
// res = "x86_64 80"

Check:

pic

Of course, you can use every func(string) string function, not just os.Getenv

Date arithmetics
t := time.Date(2018, 1, 18, 22, 57, 37, 12, time.UTC)
res := format.Formatm("${ date + 1 day | %Y-%m-%d %H:%M:%S }", format.Values{
	"date": t,
})
// res = "2018-01-19 22:57:37"

Date arithmetics allows following expression values:

  • year/years
  • month/months
  • week/weeks
  • day/days
  • hour/hours
  • minute/minutes
  • second/seconds

Where year and years (and other couples) are equivalent (5 years is nicer than 5 year, just like 1 year is prefered over 1 years). There's a limitation though, longer period must precedes shorter one, i.e. following expressions are valid.

+ 1 year + 5 weeks + 3 days + 12 seconds
+ 25 years + 3 months
- 17 days - 16 hours
+ 2 year + 15 weeks + 1 second

and this one is invalid

+ 1 week + 5 months

as a month must precedes a week

Low level usage, how it is doing in the background
package main

import (
	"fmt"
	"time"
	
	"github.com/sirkon/go-format"
)

func main() {
	bctx := format.NewContextBuilder()
	bctx.AddString("name", "explosion")
	bctx.AddInt("count", 15)
	bctx.AddTime("time", time.Date(2006, 1, 2, 3, 4, 5, 0, time.UTC))
	ctx, err := bctx.Build()
	if err != nil {
		panic(err)
	}
	
	res, err := format.Format(
		"${name} will be registered by $count independent sources in ${ time + 1 day | %Y-%m-%d } at ${ time | %H:%M:%S }",
		ctx)
	if err != nil {
		panic(err)
	}
	fmt.Println(res)
}

It is probably to be used for most typical usecases.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(format string, context Context) (string, error)

Format function

func Formatf

func Formatf(format string, data func(string) string) string

Formatf is a formatting where values are taken form given func(string) string function

func Formatg

func Formatg(format string, data interface{}) string

Formatg is a formatting with type guessing

func Formatm

func Formatm(format string, data Values) string

Formatm is a formatting with keys from given map

func Formatp

func Formatp(format string, a ...interface{}) string

Formatp is a formatting with positional arguments

Types

type Context

type Context interface {
	GetFormatter(name string) (Formatter, error)
}

Context ...

type ContextBuilder

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

ContextBuilder formatting context builder

func NewContextBuilder

func NewContextBuilder() *ContextBuilder

NewContextBuilder ...

func (*ContextBuilder) Add

func (c *ContextBuilder) Add(name string, value interface{}) *ContextBuilder

Add adds formatter with type guessing

func (*ContextBuilder) AddFloat32

func (c *ContextBuilder) AddFloat32(name string, value float32) *ContextBuilder

AddFloat adds floating point number formatter

func (*ContextBuilder) AddFloat64

func (c *ContextBuilder) AddFloat64(name string, value float64) *ContextBuilder

AddFloat adds floating point number formatter

func (*ContextBuilder) AddFormatter

func (c *ContextBuilder) AddFormatter(name string, formatter Formatter) *ContextBuilder

AddFormatter ...

func (*ContextBuilder) AddInt

func (c *ContextBuilder) AddInt(name string, value int) *ContextBuilder

AddInt adds int formatter

func (*ContextBuilder) AddInt16

func (c *ContextBuilder) AddInt16(name string, value int16) *ContextBuilder

AddInt16 adds int16 formatter

func (*ContextBuilder) AddInt32

func (c *ContextBuilder) AddInt32(name string, value int32) *ContextBuilder

AddInt32 adds int32 formatter

func (*ContextBuilder) AddInt64

func (c *ContextBuilder) AddInt64(name string, value int64) *ContextBuilder

AddInt64 adds int64 formatter

func (*ContextBuilder) AddInt8

func (c *ContextBuilder) AddInt8(name string, value int8) *ContextBuilder

AddInt8 adds int8 formatter

func (*ContextBuilder) AddString

func (c *ContextBuilder) AddString(name, value string) *ContextBuilder

AddString adds string formatter

func (*ContextBuilder) AddTime

func (c *ContextBuilder) AddTime(name string, datetime time.Time) *ContextBuilder

AddTime adds time formatter

func (*ContextBuilder) AddUint

func (c *ContextBuilder) AddUint(name string, value uint) *ContextBuilder

AddUint adds uint formatter

func (*ContextBuilder) AddUint16

func (c *ContextBuilder) AddUint16(name string, value uint16) *ContextBuilder

AddUint16 adds uint16 formatter

func (*ContextBuilder) AddUint32

func (c *ContextBuilder) AddUint32(name string, value uint32) *ContextBuilder

AddUint32 adds uint32 formatter

func (*ContextBuilder) AddUint64

func (c *ContextBuilder) AddUint64(name string, value uint64) *ContextBuilder

AddUint64 adds uint64 formatter

func (*ContextBuilder) AddUint8

func (c *ContextBuilder) AddUint8(name string, value uint8) *ContextBuilder

AddUint8 adds uint8 formatter

func (*ContextBuilder) AddValue

func (c *ContextBuilder) AddValue(name string, value interface{}) *ContextBuilder

AddValue adds formatter to format it as %...v

func (*ContextBuilder) Build

func (c *ContextBuilder) Build() (Context, error)

Build retrieves context object from the builder

type Formatter

type Formatter interface {
	Clarify(string) (Formatter, error)
	Format(string) (string, error)
}

Formatter generic formatting piece

type Splitter

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

Splitter splits the string

func NewSplitter

func NewSplitter(text string, context Context) *Splitter

NewSplitter ...

func (*Splitter) Err

func (s *Splitter) Err() error

Err ...

func (*Splitter) Split

func (s *Splitter) Split() bool

Split cut the next chunk and apply context formatting once it is a piece of format

func (*Splitter) Text

func (s *Splitter) Text() string

Text ...

type Values

type Values = map[string]interface{}

Values is easier than map[string]interface{}

Jump to

Keyboard shortcuts

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