Skip to content

Commit

Permalink
Adding Support for Empty Values
Browse files Browse the repository at this point in the history
**Why:**

* This allows the responses to customized if params aren't set

**This change addresses the need by:**

* closes #5

Signed-off-by: Christopher Hein <me@christopherhein.com>
  • Loading branch information
christopherhein committed Aug 7, 2018
1 parent 74881aa commit 40cb085
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package version

import (
"encoding/json"
"fmt"
"strings"
)

// Info creates a formattable struct for output
type Info struct {
Version string
Commit string
Date string
Version string `json:"Version,omitempty"`
Commit string `json:"Commit,omitempty"`
Date string `json:"Date,omitempty"`
}

// New will create a pointer to a new version object
Expand All @@ -28,7 +28,29 @@ func (v *Info) ToJSON() string {
}

// ToShortened converts the Info into a JSON String
func (v *Info) ToShortened() string {
str := fmt.Sprintf("Version: %v\nCommit: %v\nDate: %v\n", v.Version, v.Commit, v.Date)
return str
func (v *Info) ToShortened() (str string) {
var version, commit, date string
if v.Version != "" {
version = "Version: " + v.Version
}
if v.Commit != "" {
commit = "Commit: " + v.Commit
}
if v.Date != "" {
date = "Date: " + v.Date
}
values := []string{version, commit, date}
values = deleteEmpty(values)
str = strings.Join(values, "\n")
return str + "\n"
}

func deleteEmpty(s []string) []string {
var r []string
for _, str := range s {
if str != "" {
r = append(r, str)
}
}
return r
}

0 comments on commit 40cb085

Please sign in to comment.