Skip to content

Commit

Permalink
Merge similar code.
Browse files Browse the repository at this point in the history
  • Loading branch information
envygeeks committed Nov 22, 2018
1 parent d7e6fc0 commit fe2cf56
Show file tree
Hide file tree
Showing 22 changed files with 750 additions and 889 deletions.
44 changes: 44 additions & 0 deletions flags/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,47 @@ type Args map[string]interface {
func New() Args {
return make(Args)
}

// Bool pulls a value as a boolean, this
// supports *bool, and bool, because either
// can be given depending on flags
func (a Args) Bool(k string) bool {
o, ok := a[k].(bool)
if ok {
return o
}

v := *a[k].(*bool)
return bool(v)
}

// String pulls a value as a boolean, this
// supports *bool, and bool, because either
// can be given depending on flags
func (a Args) String(k string) string {
v, ok := a[k]
if !ok {
return ""
}

if s, ok := v.(string); !ok {
if s, ok := v.(*string); ok && s != nil {
return string(*s)
}
} else {
return s
}

return ""
}

// IsEmpty Allows you to check if a value was
// given, this means that it wasn't nil, that it
// exists on the map, and that it's not empty.
func (a Args) IsEmpty(k string) bool {
if v := a.String(k); v == "" {
return true
}

return false
}
87 changes: 87 additions & 0 deletions flags/args/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,90 @@ func TestNew(t *testing.T) {
assert.IsType(t, expected,
actual)
}

func TestBool(t *testing.T) {
type TestStruct struct {
input *bool
description string
expected bool
}

for _, ts := range []TestStruct{
TestStruct{
input: &[]bool{false}[0],
description: "it's false when false",
expected: false,
},
TestStruct{
input: &[]bool{true}[0],
description: "it's true when true",
expected: true,
},
} {
args := Args{
"k": ts.input,
}

actual := args.Bool("k")
assert.Equal(t, ts.expected, actual,
ts.description)
}
}

func TestString(t *testing.T) {
type TestStruct struct {
input *string
description string
expected string
}

for _, ts := range []TestStruct{
TestStruct{
input: &[]string{""}[0],
description: "it returns a string",
expected: "",
},
} {
args := Args{
"k": ts.input,
}

actual := args.String("k")
assert.Equal(t, ts.expected, actual,
ts.description)
}
}

func TestIsEmpty(t *testing.T) {
type TestStruct struct {
input *string
description string
expected bool
}

for _, ts := range []TestStruct{
TestStruct{
expected: true,
description: "is true on nil",
input: nil,
},
TestStruct{
input: &[]string{"string"}[0],
description: "it's false on non-empty string",
expected: false,
},
TestStruct{
input: &[]string{""}[0],
description: "it's true on \"\"",
expected: true,
},
} {
args := Args{
"k": ts.input,
}

actual := args.IsEmpty("k")
assert.Equal(t, ts.expected, actual,
ts.description)
}
}
38 changes: 0 additions & 38 deletions flags/args/getters.go

This file was deleted.

63 changes: 0 additions & 63 deletions flags/args/getters_test.go

This file was deleted.

16 changes: 0 additions & 16 deletions flags/args/test.go

This file was deleted.

44 changes: 0 additions & 44 deletions flags/args/test_test.go

This file was deleted.

40 changes: 0 additions & 40 deletions template/handle.go

This file was deleted.

Loading

0 comments on commit fe2cf56

Please sign in to comment.