Skip to content

Commit

Permalink
Get now returns a strong only. GetIf returns a (string, bool)
Browse files Browse the repository at this point in the history
  • Loading branch information
karlseguin committed Sep 2, 2015
1 parent 20ed02d commit 14b7f0e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,5 +1,5 @@
t:
go test ./...
go test ./... -v

f:
go fmt ./...
2 changes: 1 addition & 1 deletion arrayparams.go
Expand Up @@ -9,7 +9,7 @@ type ArrayParams struct {
}

// Get a value by key
func (p *ArrayParams) Get(key string) (string, bool) {
func (p *ArrayParams) GetIf(key string) (string, bool) {
position, exists := p.indexOf(key)
if exists == false {
return "", false
Expand Down
4 changes: 2 additions & 2 deletions mapparams.go
Expand Up @@ -2,7 +2,7 @@ package params

type MapParams map[string]string

func (p MapParams) Get(key string) (string, bool) {
func (p MapParams) GetIf(key string) (string, bool) {
v, k := p[key]
return v, k
}
Expand All @@ -13,7 +13,7 @@ func (p MapParams) Set(key, value string) bool {
}

func (p MapParams) Delete(key string) (string, bool) {
value, exists := p.Get(key)
value, exists := p.GetIf(key)
if exists {
delete(p, key)
}
Expand Down
11 changes: 8 additions & 3 deletions params.go
Expand Up @@ -3,7 +3,7 @@ package params

// An interface to a key-value lookup
type params interface {
Get(key string) (string, bool)
GetIf(key string) (string, bool)
Set(key, value string) bool
ToMap(key, value string) params
Delete(key string) (string, bool)
Expand Down Expand Up @@ -34,8 +34,13 @@ func pooled(pool *Pool, length int) *Params {
}
}

func (p *Params) Get(key string) (string, bool) {
return p.current.Get(key)
func (p *Params) Get(key string) string {
value, _ := p.GetIf(key)
return value
}

func (p *Params) GetIf(key string) (string, bool) {
return p.current.GetIf(key)
}

func (p *Params) Set(key, value string) {
Expand Down
32 changes: 18 additions & 14 deletions params_test.go
@@ -1,8 +1,9 @@
package params

import (
. "github.com/karlseguin/expect"
"testing"

. "github.com/karlseguin/expect"
)

type ArrayParamsTests struct{}
Expand All @@ -12,34 +13,37 @@ func Test_ArrayParams(t *testing.T) {
}

func (_ ArrayParamsTests) GetsFromEmpty() {
Expect(New(1).Get("baron:friends")).To.Equal("", false)
Expect(New(1).GetIf("baron:friends")).To.Equal("", false)
}

func (_ ArrayParamsTests) GetsAValue() {
p := New(10)
p.Set("leto", "ghanima")
p.Set("paul", "alia")
p.Set("duncan", "")
Expect(p.Get("leto")).To.Equal("ghanima", true)
Expect(p.Get("paul")).To.Equal("alia", true)
Expect(p.Get("duncan")).To.Equal("", true)
Expect(p.Get("vladimir")).To.Equal("", false)
Expect(p.GetIf("leto")).To.Equal("ghanima", true)
Expect(p.GetIf("paul")).To.Equal("alia", true)
Expect(p.GetIf("duncan")).To.Equal("", true)
Expect(p.GetIf("vladimir")).To.Equal("", false)

Expect(p.Get("paul")).To.Equal("alia")
Expect(p.Get("vladimir")).To.Equal("")
}

func (_ ArrayParamsTests) OverwritesAnExistingValue() {
p := New(10)
p.Set("leto", "ghaima")
p.Set("leto", "ghanima")
Expect(p.Get("leto")).To.Equal("ghanima", true)
Expect(p.GetIf("leto")).To.Equal("ghanima", true)
}

func (_ ArrayParamsTests) ExpandsBeyondTheSpecifiedSize() {
p := New(1)
p.Set("leto", "ghanima")
p.Set("paul", "alia")
Expect(p.Get("leto")).To.Equal("ghanima", true)
Expect(p.Get("paul")).To.Equal("alia", true)
Expect(p.Get("vladimir")).To.Equal("", false)
Expect(p.GetIf("leto")).To.Equal("ghanima", true)
Expect(p.GetIf("paul")).To.Equal("alia", true)
Expect(p.GetIf("vladimir")).To.Equal("", false)
_, ok := p.current.(MapParams)
Expect(ok).ToEqual(true)
}
Expand All @@ -63,7 +67,7 @@ func (_ ArrayParamsTests) ClearsTheParam() {
p.Set("paul", "alia")
p.Clear()
Expect(p.Len()).To.Equal(0)
Expect(p.Get("leto")).To.Equal("", false)
Expect(p.GetIf("leto")).To.Equal("", false)
}

func (_ ArrayParamsTests) DeletesFromNothing() {
Expand Down Expand Up @@ -91,7 +95,7 @@ func (_ ArrayParamsTests) DeletesAnItem() {
p.Set("duncan", "")
Expect(p.Delete("leto")).To.Equal("ghanima", true)
Expect(p.Len()).To.Equal(2)
Expect(p.Get("duncan")).To.Equal("", true)
Expect(p.Get("paul")).To.Equal("alia", true)
Expect(p.Get("leto")).To.Equal("", false)
Expect(p.GetIf("duncan")).To.Equal("", true)
Expect(p.GetIf("paul")).To.Equal("alia", true)
Expect(p.GetIf("leto")).To.Equal("", false)
}

0 comments on commit 14b7f0e

Please sign in to comment.