Skip to content

Commit

Permalink
Remove special aws.Time structure.
Browse files Browse the repository at this point in the history
No longer needed due to custom JSON marshaling.

Also replace with aws.Time(t time.Time) helper to generate a
Time value pointer.

Fixes aws#101
  • Loading branch information
lsegal committed Mar 18, 2015
1 parent 1db984f commit 523a181
Show file tree
Hide file tree
Showing 23 changed files with 305 additions and 371 deletions.
45 changes: 8 additions & 37 deletions aws/types.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,28 @@
package aws

import (
"math"
"strconv"
"time"
)
import "time"

// String converts a Go string into a StringValue.
// String converts a Go string into a string pointer.
func String(v string) *string {
return &v
}

// Boolean converts a Go bool into a BooleanValue.
// Boolean converts a Go bool into a boolean pointer.
func Boolean(v bool) *bool {
return &v
}

// Long converts a Go int64 into a LongValue.
// Long converts a Go int64 into a long pointer.
func Long(v int64) *int64 {
return &v
}

// Double converts a Go float64 into a DoubleValue.
// Double converts a Go float64 into a double pointer.
func Double(v float64) *float64 {
return &v
}

func NewTime(t time.Time) *Time {
return &Time{t}
}

// A Time is a timestamp
type Time struct {
time.Time
}

// MarshalJSON marshals the timestamp as a float.
func (t Time) MarshalJSON() (text []byte, err error) {
n := float64(t.Time.UnixNano()) / 1e9
s := strconv.FormatFloat(n, 'f', -1, 64)
return []byte(s), nil
}

// UnmarshalJSON unmarshals the timestamp from a float.
func (t *Time) UnmarshalJSON(text []byte) error {
f, err := strconv.ParseFloat(string(text), 64)
if err != nil {
return err
}

sec := math.Floor(f)
nsec := (f - sec) * 1e9

t.Time = time.Unix(int64(sec), int64(nsec)).UTC()
return nil
// Time converts a Go Time into a Time pointer
func Time(t time.Time) *time.Time {
return &t
}
31 changes: 0 additions & 31 deletions aws/types_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/fixtures/helpers/param_filler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (f paramFiller) paramsStructAny(value interface{}, shape *api.Shape) string
case "timestamp":
v := reflect.Indirect(reflect.ValueOf(value))
if v.IsValid() {
return fmt.Sprintf("aws.NewTime(time.Unix(%d, 0))", int(v.Float()))
return fmt.Sprintf("aws.Time(time.Unix(%d, 0))", int(v.Float()))
}
default:
panic("Unhandled type " + shape.Type)
Expand Down
2 changes: 1 addition & 1 deletion internal/model/api/shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *Shape) GoType() string {
return "*float64"
case "timestamp":
s.API.imports["time"] = true
return "*aws.Time"
return "*time.Time"
default:
panic("Unsupported shape type: " + s.Type)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/protocol/ec2query/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func (c *InputService8ProtocolTest) InputService8TestCaseOperation1(input *Input
var opInputService8TestCaseOperation1 *aws.Operation

type InputService8TestShapeInputService8TestCaseOperation1Input struct {
TimeArg *aws.Time `type:"timestamp" timestampFormat:"iso8601"`
TimeArg *time.Time `type:"timestamp" timestampFormat:"iso8601"`

metadataInputService8TestShapeInputService8TestCaseOperation1Input `json:"-", xml:"-"`
}
Expand Down Expand Up @@ -833,7 +833,7 @@ func TestInputService8ProtocolTestTimestampValuesCase1(t *testing.T) {
svc.Endpoint = "https://test"

input := &InputService8TestShapeInputService8TestCaseOperation1Input{
TimeArg: aws.NewTime(time.Unix(1422172800, 0)),
TimeArg: aws.Time(time.Unix(1422172800, 0)),
}
req, _ := svc.InputService8TestCaseOperation1Request(input)
r := req.HTTPRequest
Expand Down
28 changes: 14 additions & 14 deletions internal/protocol/ec2query/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ func NewOutputService1ProtocolTest(config *OutputService1ProtocolTestConfig) *Ou
}

// OutputService1TestCaseOperation1Request generates a request for the OutputService1TestCaseOperation1 operation.
func (c *OutputService1ProtocolTest) OutputService1TestCaseOperation1Request(input *OutputService1TestShapeOutputService1TestShapeOutputService1TestCaseOperation1Input) (req *aws.Request, output *OutputService1TestShapeOutputService1TestCaseOperation1Output) {
func (c *OutputService1ProtocolTest) OutputService1TestCaseOperation1Request(input *OutputService1TestShapeOutputService1TestCaseOperation1Input) (req *aws.Request, output *OutputService1TestShapeOutputService1TestShapeOutputService1TestCaseOperation1Output) {
if opOutputService1TestCaseOperation1 == nil {
opOutputService1TestCaseOperation1 = &aws.Operation{
Name: "OperationName",
}
}

req = aws.NewRequest(c.Service, opOutputService1TestCaseOperation1, input, output)
output = &OutputService1TestShapeOutputService1TestCaseOperation1Output{}
output = &OutputService1TestShapeOutputService1TestShapeOutputService1TestCaseOperation1Output{}
req.Data = output
return
}

func (c *OutputService1ProtocolTest) OutputService1TestCaseOperation1(input *OutputService1TestShapeOutputService1TestShapeOutputService1TestCaseOperation1Input) (output *OutputService1TestShapeOutputService1TestCaseOperation1Output, err error) {
func (c *OutputService1ProtocolTest) OutputService1TestCaseOperation1(input *OutputService1TestShapeOutputService1TestCaseOperation1Input) (output *OutputService1TestShapeOutputService1TestShapeOutputService1TestCaseOperation1Output, err error) {
req, out := c.OutputService1TestCaseOperation1Request(input)
output = out
err = req.Send()
Expand All @@ -83,7 +83,15 @@ func (c *OutputService1ProtocolTest) OutputService1TestCaseOperation1(input *Out

var opOutputService1TestCaseOperation1 *aws.Operation

type OutputService1TestShapeOutputService1TestCaseOperation1Output struct {
type OutputService1TestShapeOutputService1TestCaseOperation1Input struct {
metadataOutputService1TestShapeOutputService1TestCaseOperation1Input `json:"-", xml:"-"`
}

type metadataOutputService1TestShapeOutputService1TestCaseOperation1Input struct {
SDKShapeTraits bool `type:"structure"`
}

type OutputService1TestShapeOutputService1TestShapeOutputService1TestCaseOperation1Output struct {
Char *string `type:"character"`
Double *float64 `type:"double"`
FalseBool *bool `type:"boolean"`
Expand All @@ -93,18 +101,10 @@ type OutputService1TestShapeOutputService1TestCaseOperation1Output struct {
Str *string `type:"string"`
TrueBool *bool `type:"boolean"`

metadataOutputService1TestShapeOutputService1TestCaseOperation1Output `json:"-", xml:"-"`
}

type metadataOutputService1TestShapeOutputService1TestCaseOperation1Output struct {
SDKShapeTraits bool `type:"structure"`
}

type OutputService1TestShapeOutputService1TestShapeOutputService1TestCaseOperation1Input struct {
metadataOutputService1TestShapeOutputService1TestShapeOutputService1TestCaseOperation1Input `json:"-", xml:"-"`
metadataOutputService1TestShapeOutputService1TestShapeOutputService1TestCaseOperation1Output `json:"-", xml:"-"`
}

type metadataOutputService1TestShapeOutputService1TestShapeOutputService1TestCaseOperation1Input struct {
type metadataOutputService1TestShapeOutputService1TestShapeOutputService1TestCaseOperation1Output struct {
SDKShapeTraits bool `type:"structure"`
}

Expand Down
9 changes: 4 additions & 5 deletions internal/protocol/json/jsonutil/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"sort"
"strconv"
"strings"

"github.com/awslabs/aws-sdk-go/aws"
"time"
)

func BuildJSON(v interface{}) ([]byte, error) {
Expand All @@ -32,7 +31,7 @@ func buildAny(value reflect.Value, buf *bytes.Buffer, tag reflect.StructTag) err
switch vtype.Kind() {
case reflect.Struct:
// also it can't be a time object
if _, ok := value.Interface().(aws.Time); !ok {
if _, ok := value.Interface().(time.Time); !ok {
t = "structure"
}
case reflect.Slice:
Expand Down Expand Up @@ -161,8 +160,8 @@ func buildScalar(value reflect.Value, buf *bytes.Buffer, tag reflect.StructTag)
buf.WriteString(strconv.FormatInt(converted, 10))
case float64:
buf.WriteString(strconv.FormatFloat(converted, 'f', -1, 64))
case aws.Time:
buf.WriteString(strconv.FormatInt(converted.Time.UTC().Unix(), 10))
case time.Time:
buf.WriteString(strconv.FormatInt(converted.UTC().Unix(), 10))
default:
return fmt.Errorf("unsupported JSON value %v (%s)", value.Interface(), value.Type())
}
Expand Down
8 changes: 3 additions & 5 deletions internal/protocol/json/jsonutil/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"reflect"
"strings"
"time"

"github.com/awslabs/aws-sdk-go/aws"
)

func UnmarshalJSON(v interface{}, stream io.Reader) error {
Expand Down Expand Up @@ -39,7 +37,7 @@ func unmarshalAny(value reflect.Value, data interface{}, tag reflect.StructTag)
switch vtype.Kind() {
case reflect.Struct:
// also it can't be a time object
if _, ok := value.Interface().(*aws.Time); !ok {
if _, ok := value.Interface().(*time.Time); !ok {
t = "structure"
}
case reflect.Slice:
Expand Down Expand Up @@ -198,9 +196,9 @@ func unmarshalScalar(value reflect.Value, data interface{}, tag reflect.StructTa
value.Set(reflect.ValueOf(&di))
case *float64:
value.Set(reflect.ValueOf(&d))
case *aws.Time:
case *time.Time:
t := time.Unix(int64(d), 0).UTC()
value.Set(reflect.ValueOf(aws.NewTime(t)))
value.Set(reflect.ValueOf(&t))
default:
return errf()
}
Expand Down
Loading

0 comments on commit 523a181

Please sign in to comment.