[config] update glide dependencies

This commit is contained in:
Alex Quick
2017-07-19 09:59:44 -04:00
parent 8e8b6d60f0
commit a13722eec1
9 changed files with 128 additions and 56 deletions

View File

@@ -56,12 +56,14 @@ config.Delim = "|"
config.Glue = " "
config.Prefix = ""
config.Empty = ""
config.NoTrim = false
```
* `Delim` is the string by which columns of **input** are delimited
* `Glue` is the string by which columns of **output** are delimited
* `Prefix` is a string by which each line of **output** is prefixed
* `Empty` is a string used to replace blank values found in output
* `NoTrim` is a boolean used to disable the automatic trimming of input values
You can then pass the `Config` in using the `Format` method (signature below) to
have text formatted to your liking.

View File

@@ -18,8 +18,11 @@ type Config struct {
// The string by which columns of output will be prefixed.
Prefix string
// A replacement string to replace empty fields
// A replacement string to replace empty fields.
Empty string
// NoTrim disables automatic trimming of inputs.
NoTrim bool
}
// DefaultConfig returns a *Config with default values.
@@ -29,18 +32,22 @@ func DefaultConfig() *Config {
Glue: " ",
Prefix: "",
Empty: "",
NoTrim: false,
}
}
// MergeConfig merges two config objects together and returns the resulting
// configuration. Values from the right take precedence over the left side.
func MergeConfig(a, b *Config) *Config {
var result Config = *a
// Return quickly if either side was nil
if a == nil || b == nil {
return &result
if a == nil {
return b
}
if b == nil {
return a
}
var result Config = *a
if b.Delim != "" {
result.Delim = b.Delim
@@ -54,6 +61,9 @@ func MergeConfig(a, b *Config) *Config {
if b.Empty != "" {
result.Empty = b.Empty
}
if b.NoTrim {
result.NoTrim = true
}
return &result
}
@@ -83,10 +93,13 @@ func stringFormat(c *Config, widths []int, columns int) string {
// elementsFromLine returns a list of elements, each representing a single
// item which will belong to a column of output.
func elementsFromLine(config *Config, line string) []interface{} {
seperated := strings.Split(line, config.Delim)
elements := make([]interface{}, len(seperated))
for i, field := range seperated {
value := strings.TrimSpace(field)
separated := strings.Split(line, config.Delim)
elements := make([]interface{}, len(separated))
for i, field := range separated {
value := field
if !config.NoTrim {
value = strings.TrimSpace(field)
}
// Apply the empty value, if configured.
if value == "" && config.Empty != "" {

View File

@@ -2,6 +2,7 @@ package columnize
import (
"fmt"
"reflect"
"testing"
crand "crypto/rand"
@@ -155,6 +156,24 @@ func TestVariedInputSpacing(t *testing.T) {
}
}
func TestVariedInputSpacing_NoTrim(t *testing.T) {
input := []string{
"Column A|Column B|Column C",
"x|y| z",
}
config := DefaultConfig()
config.NoTrim = true
output := Format(input, config)
expected := "Column A Column B Column C\n"
expected += "x y z"
if output != expected {
t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output)
}
}
func TestUnmatchedColumnCounts(t *testing.T) {
input := []string{
"Column A | Column B | Column C",
@@ -280,27 +299,42 @@ func TestEmptyConfigValues(t *testing.T) {
}
func TestMergeConfig(t *testing.T) {
conf1 := &Config{Delim: "a", Glue: "a", Prefix: "a", Empty: "a"}
conf2 := &Config{Delim: "b", Glue: "b", Prefix: "b", Empty: "b"}
conf3 := &Config{Delim: "c", Prefix: "c"}
m := MergeConfig(conf1, conf2)
if m.Delim != "b" || m.Glue != "b" || m.Prefix != "b" || m.Empty != "b" {
t.Fatalf("bad: %#v", m)
}
m = MergeConfig(conf1, conf3)
if m.Delim != "c" || m.Glue != "a" || m.Prefix != "c" || m.Empty != "a" {
t.Fatalf("bad: %#v", m)
}
m = MergeConfig(conf1, nil)
if m.Delim != "a" || m.Glue != "a" || m.Prefix != "a" || m.Empty != "a" {
t.Fatalf("bad: %#v", m)
}
m = MergeConfig(conf1, &Config{})
if m.Delim != "a" || m.Glue != "a" || m.Prefix != "a" || m.Empty != "a" {
t.Fatalf("bad: %#v", m)
for _, tc := range []struct {
desc string
configA *Config
configB *Config
expect *Config
}{
{
"merges b over a",
&Config{Delim: "a", Glue: "a", Prefix: "a", Empty: "a"},
&Config{Delim: "b", Glue: "b", Prefix: "b", Empty: "b"},
&Config{Delim: "b", Glue: "b", Prefix: "b", Empty: "b"},
},
{
"merges only non-empty config values",
&Config{Delim: "a", Glue: "a", Prefix: "a", Empty: "a"},
&Config{Delim: "b", Prefix: "b"},
&Config{Delim: "b", Glue: "a", Prefix: "b", Empty: "a"},
},
{
"takes b if a is nil",
nil,
&Config{Delim: "b", Glue: "b", Prefix: "b", Empty: "b"},
&Config{Delim: "b", Glue: "b", Prefix: "b", Empty: "b"},
},
{
"takes a if b is nil",
&Config{Delim: "a", Glue: "a", Prefix: "a", Empty: "a"},
nil,
&Config{Delim: "a", Glue: "a", Prefix: "a", Empty: "a"},
},
} {
t.Run(tc.desc, func(t *testing.T) {
m := MergeConfig(tc.configA, tc.configB)
if !reflect.DeepEqual(m, tc.expect) {
t.Fatalf("\nexpect:\n%#v\n\nactual:\n%#v", tc.expect, m)
}
})
}
}