Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add json Unmarshal for AWSEpochTime #1298

Merged
merged 2 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changelog/a6739ebd15a745c3b7284fe3b91746e9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "a6739ebd-15a7-45c3-b728-4fe3b91746e9",
"type": "feature",
"description": "Add UnmarshalJSON for AWSEpochTime to correctly unmarshal AWSEpochTime, ([#1298](https://github.com/aws/aws-sdk-go-v2/pull/1298))",
"modules": [
"feature/cloudfront/sign"
]
}
13 changes: 13 additions & 0 deletions feature/cloudfront/sign/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ func (t AWSEpochTime) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`{"AWS:EpochTime":%d}`, t.UTC().Unix())), nil
}

// UnmarshalJSON unserializes AWS Profile epoch time.
func (t *AWSEpochTime) UnmarshalJSON(data []byte) error {
var epochTime struct {
Sec int64 `json:"AWS:EpochTime"`
}
err := json.Unmarshal(data, &epochTime)
if err != nil {
return err
}
t.Time = time.Unix(epochTime.Sec, 0).UTC()
return nil
}

// An IPAddress wraps an IPAddress source IP providing JSON serialization information
type IPAddress struct {
SourceIP string `json:"AWS:SourceIp"`
Expand Down
17 changes: 17 additions & 0 deletions feature/cloudfront/sign/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/rsa"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"math/rand"
"strings"
Expand All @@ -27,6 +28,22 @@ func TestEpochTimeMarshal(t *testing.T) {
}
}

func TestEpochTimeUnmarshal(t *testing.T) {
now := time.Now().Round(time.Second)
data := fmt.Sprintf(`{"AWS:EpochTime":%d}`, now.Unix())
var v AWSEpochTime
err := json.Unmarshal([]byte(data), &v)
if err != nil {
t.Fatalf("Unexpected error, %#v", err)
}

expected := now.UTC()
if v.Time != expected {
t.Errorf("Expected unmarshaled time to match, expect: %s, actual: %s",
expected, v.Time)
}
}

var testCreateResource = []struct {
scheme, u string
expect string
Expand Down