Giter Site home page Giter Site logo

go-format's Introduction

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.

go-format's People

Contributors

sirkon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.