From 0904b1354660001a61f8aefc42c98bf103674528 Mon Sep 17 00:00:00 2001 From: Fredric Silberberg Date: Thu, 18 Sep 2014 12:48:24 -0400 Subject: [PATCH] Moved units to be with themselves in common, updated convertUnit and parseLine to be public for testing --- common.go | 38 +++++++++++++++++++++++++++----------- free/main.go | 8 ++++---- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/common.go b/common.go index 964addb..c2cb6ad 100644 --- a/common.go +++ b/common.go @@ -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) + } +} diff --git a/free/main.go b/free/main.go index 0849aef..cc8b9e5 100644 --- a/free/main.go +++ b/free/main.go @@ -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^[\\w()]+):\\s+(?P\\d+)(\\s+)?(?P\\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) }