Skip to content

Commit

Permalink
Move time utils to internal pacakge
Browse files Browse the repository at this point in the history
- A test was depending on being in the linodego package
  • Loading branch information
phillc committed Jan 23, 2020
1 parent 0465fb0 commit e0f0564
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 71 deletions.
61 changes: 3 additions & 58 deletions account_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/linode/linodego/internal/utils"
)

// Event represents an action taken on the Account.
Expand Down Expand Up @@ -195,7 +194,7 @@ func (i *Event) UnmarshalJSON(b []byte) error {
}

i.Created = (*time.Time)(p.Created)
i.TimeRemaining = unmarshalTimeRemaining(p.TimeRemaining)
i.TimeRemaining = utils.UnmarshalTimeRemaining(p.TimeRemaining)

return nil
}
Expand Down Expand Up @@ -267,57 +266,3 @@ func (c *Client) MarkEventsSeen(ctx context.Context, event *Event) error {

return err
}

func unmarshalTimeRemaining(m json.RawMessage) *int {
jsonBytes, err := m.MarshalJSON()
if err != nil {
panic(jsonBytes)
}

if len(jsonBytes) == 4 && string(jsonBytes) == "null" {
return nil
}

var timeStr string
if err := json.Unmarshal(jsonBytes, &timeStr); err == nil && len(timeStr) > 0 {
if dur, err := durationToSeconds(timeStr); err != nil {
panic(err)
} else {
return &dur
}
} else {
var intPtr int
if err := json.Unmarshal(jsonBytes, &intPtr); err == nil {
return &intPtr
}
}

log.Println("[WARN] Unexpected unmarshalTimeRemaining value: ", jsonBytes)

return nil
}

// durationToSeconds takes a hh:mm:ss string and returns the number of seconds
func durationToSeconds(s string) (int, error) {
multipliers := [3]int{60 * 60, 60, 1}
segs := strings.Split(s, ":")

if len(segs) > len(multipliers) {
return 0, fmt.Errorf("too many ':' separators in time duration: %s", s)
}

var d int

l := len(segs)

for i := 0; i < l; i++ {
m, err := strconv.Atoi(segs[i])
if err != nil {
return 0, err
}

d += m * multipliers[i+len(multipliers)-l]
}

return d, nil
}
63 changes: 63 additions & 0 deletions internal/utils/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package utils

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

func UnmarshalTimeRemaining(m json.RawMessage) *int {
jsonBytes, err := m.MarshalJSON()
if err != nil {
panic(jsonBytes)
}

if len(jsonBytes) == 4 && string(jsonBytes) == "null" {
return nil
}

var timeStr string
if err := json.Unmarshal(jsonBytes, &timeStr); err == nil && len(timeStr) > 0 {
if dur, err := durationToSeconds(timeStr); err != nil {
panic(err)
} else {
return &dur
}
} else {
var intPtr int
if err := json.Unmarshal(jsonBytes, &intPtr); err == nil {
return &intPtr
}
}

log.Println("[WARN] Unexpected unmarshalTimeRemaining value: ", jsonBytes)

return nil
}

// durationToSeconds takes a hh:mm:ss string and returns the number of seconds
func durationToSeconds(s string) (int, error) {
multipliers := [3]int{60 * 60, 60, 1}
segs := strings.Split(s, ":")

if len(segs) > len(multipliers) {
return 0, fmt.Errorf("too many ':' separators in time duration: %s", s)
}

var d int

l := len(segs)

for i := 0; i < l; i++ {
m, err := strconv.Atoi(segs[i])
if err != nil {
return 0, err
}

d += m * multipliers[i+len(multipliers)-l]
}

return d, nil
}
18 changes: 18 additions & 0 deletions internal/utils/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package utils

import (
"encoding/json"
"testing"
)

func TestUnmarshalTimeRemaining(t *testing.T) {
if *UnmarshalTimeRemaining(json.RawMessage("\"1:23\"")) != 83 {
t.Errorf("Error parsing duration style time_remaining")
}
if UnmarshalTimeRemaining(json.RawMessage("null")) != nil {
t.Errorf("Error parsing null time_remaining")
}
if *UnmarshalTimeRemaining(json.RawMessage("0")) != 0 {
t.Errorf("Error parsing int style time_remaining")
}
}
13 changes: 0 additions & 13 deletions test/integration/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package integration

import (
"context"
"encoding/json"
"fmt"
"testing"

Expand Down Expand Up @@ -42,15 +41,3 @@ func TestResourceTemplatedendpointWithID(t *testing.T) {
t.Errorf("Backups endpoint did not contain backup ID '%d'", backupID)
}
}

func TestUnmarshalTimeRemaining(t *testing.T) {
if *unmarshalTimeRemaining(json.RawMessage("\"1:23\"")) != 83 {
t.Errorf("Error parsing duration style time_remaining")
}
if unmarshalTimeRemaining(json.RawMessage("null")) != nil {
t.Errorf("Error parsing null time_remaining")
}
if *unmarshalTimeRemaining(json.RawMessage("0")) != 0 {
t.Errorf("Error parsing int style time_remaining")
}
}

0 comments on commit e0f0564

Please sign in to comment.