Skip to content

Commit

Permalink
Merge branch 'feat-1'
Browse files Browse the repository at this point in the history
  • Loading branch information
sirkon committed Jan 21, 2019
2 parents 3b63e47 + 9ca3877 commit bcd75ea
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 70 deletions.
39 changes: 0 additions & 39 deletions Gopkg.lock

This file was deleted.

30 changes: 0 additions & 30 deletions Gopkg.toml

This file was deleted.

15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ 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:


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

![pic](Untitled.png)

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

##### Date arithmetics
```go
t := time.Date(2018, 1, 18, 22, 57, 37, 12, time.UTC)
Expand Down
Binary file added Untitled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ func (ctx contextFromBuilder) GetFormatter(name string) (res Formatter, err erro
}
return
}

// contextFunc implementation of context using given func as a source of information
type contextFunc func(string) string

func (f contextFunc) GetFormatter(name string) (Formatter, error) {
value := f(name)
return stringFormatter(value), nil
}
10 changes: 10 additions & 0 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,13 @@ func Formatg(format string, data interface{}) string {
}
return res
}

// Formatf is a formatting where values are taken form given func(string) string function
func Formatf(format string, data func(string) string) string {
ctx := contextFunc(data)
res, err := Format(format, ctx)
if err != nil {
panic(err)
}
return res
}
36 changes: 35 additions & 1 deletion format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestClarify(t *testing.T) {
}
}

func TestFormatf(t *testing.T) {
func TestFormatp(t *testing.T) {
require.Equal(t, "a 2 4.5 2 2018", Formatp("${} ${} ${|1.1} ${1} ${|%Y}", "a", 2, 4.5, time.Date(2018, 10, 19, 18, 0, 5, 0, time.UTC)))
require.Equal(t, "a a", Formatp("$ $0", "a"))
}
Expand Down Expand Up @@ -201,3 +201,37 @@ func TestReadmeDateArithmetics(t *testing.T) {
})
require.Equal(t, "2018-01-19 22:57:37", res)
}

func TestFormatf(t *testing.T) {
tests := []struct {
name string
format string
data func(string) string
want string
}{
{
name: "typical",
format: "${name} ${value} ${location} - ${fake}",
data: func(s string) string {
switch s {
case "name":
return "Denis"
case "value":
return "1k"
case "location":
return "cbx"
default:
return "<nil>"
}
},
want: "Denis 1k cbx - <nil>",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Formatf(tt.format, tt.data); got != tt.want {
t.Errorf("Formatf() = %v, want %v", got, tt.want)
}
})
}
}
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/sirkon/go-format

require (
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
github.com/lestrrat/go-envload v0.0.0-20180220120943-6ed08b54a570 // indirect
github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042
github.com/pkg/errors v0.8.1 // indirect
github.com/stretchr/testify v1.3.0
github.com/tebeka/strftime v0.0.0-20140926081919-3f9c7761e312 // indirect
golang.org/x/net v0.0.0-20190119204137-ed066c81e75e // indirect
)

0 comments on commit bcd75ea

Please sign in to comment.