Skip to content

Commit

Permalink
Add parse string function
Browse files Browse the repository at this point in the history
  • Loading branch information
dynastymasra committed Mar 24, 2020
1 parent 48dcbb9 commit ec52cea
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,19 @@ func Stringify(str interface{}) string {
}
return string(out)
}

// ParseStringNil function to check string if empty return nil
func ParseStringNil(s string) *string {
if len(s) > 0 {
return &s
}
return nil
}

// ParsePtrString function check pointer string, if nil return empty string
func ParsePtrString(s *string) string {
if s == nil {
return ""
}
return *s
}
31 changes: 31 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,34 @@ func (p *ParseSuite) Test_Stringify_Failed_Marshal() {

assert.NotEmpty(p.T(), result)
}

func (p *ParseSuite) Test_ParseStringNil() {
s := "test"

result := cookbook.ParseStringNil(s)

assert.NotNil(p.T(), result)
}

func (p *ParseSuite) Test_ParseStringNil_Empty() {
s := ""

result := cookbook.ParseStringNil(s)

assert.Nil(p.T(), result)
}

func (p *ParseSuite) Test_ParsePtrString() {
s := "test"

result := cookbook.ParsePtrString(&s)

assert.NotEmpty(p.T(), result)
assert.Equal(p.T(), s, result)
}

func (p *ParseSuite) Test_ParsePtrString_Nil() {
result := cookbook.ParsePtrString(nil)

assert.Empty(p.T(), result)
}

0 comments on commit ec52cea

Please sign in to comment.