Moved units to be with themselves in common, updated convertUnit and parseLine to be public for testing

This commit is contained in:
Fredric Silberberg 2014-09-18 12:48:24 -04:00
parent 5122c83609
commit 0904b13546
2 changed files with 31 additions and 15 deletions

View File

@ -12,17 +12,6 @@ type JshFrame struct {
StdErr interface{}
}
// Size prefixes as integers, using binary representation
type Unit int
const (
B Unit = 2 ^ 0
KB Unit = 2 ^ 10
MB Unit = 2 ^ 20
GB Unit = 2 ^ 30
TB Unit = 2 ^ 40
)
// Converts a JshFrame into a JSON string
func (j JshFrame) ToJson() *string {
jsonOut, err := json.Marshal(j)
@ -54,3 +43,30 @@ func OutputFrames(queue chan *JshFrame, done chan bool) {
}
}
}
// Size prefixes as integers, using binary representation
type Unit int
const (
B Unit = 2 ^ 0
KB Unit = 2 ^ 10
MB Unit = 2 ^ 20
GB Unit = 2 ^ 30
TB Unit = 2 ^ 40
)
func (u Unit) ToString() string {
if u == B {
return "B"
} else if u == KB {
return "kB"
} else if u == MB {
return "mB"
} else if u == GB {
return "gB"
} else if u == TB {
return "tB"
} else {
return fmt.Sprintf("Unknown type, values is %d", u)
}
}

View File

@ -11,7 +11,7 @@ import (
"strconv"
)
func convertUnit(stringUnit string) (jsh.Unit, error) {
func ConvertUnit(stringUnit string) (jsh.Unit, error) {
switch stringUnit {
case "B":
return jsh.B, nil
@ -30,7 +30,7 @@ func convertUnit(stringUnit string) (jsh.Unit, error) {
}
}
func parseLine(line string) (string, jsh.MemStat, error) {
func ParseLine(line string) (string, jsh.MemStat, error) {
// Recognizes a alphanumeric or () word, the : character, whitespace, the number we're looking for,
// more whitespace, and possibly the unit
lineRegex := regexp.MustCompile("(?P<key>^[\\w()]+):\\s+(?P<val>\\d+)(\\s+)?(?P<unit>\\w+)?")
@ -48,7 +48,7 @@ func parseLine(line string) (string, jsh.MemStat, error) {
if err != nil {
return "", jsh.MemStat{}, err
}
unit, err := convertUnit(matchedVals["unit"])
unit, err := ConvertUnit(matchedVals["unit"])
if err != nil {
return "", jsh.MemStat{}, err
}
@ -67,7 +67,7 @@ func parseMemInfo() jsh.JshFrame {
errors := []error{}
// Read in each line of the meminfo file, and place it in the map
for scanner.Scan() {
key, val, err := parseLine(scanner.Text())
key, val, err := ParseLine(scanner.Text())
if err != nil {
errors = append(errors, err)
}