Permalink
Browse files

reorganizing code

  • Loading branch information...
1 parent 8810c48 commit 3cf4e21a5e9786d8487291f86aa101f62f235c90 @fabiokung committed Aug 31, 2012
Showing with 111 additions and 102 deletions.
  1. +10 −99 dynamodb.go
  2. +3 −3 dynamodb_test.go
  3. +98 −0 json.go
View
@@ -2,12 +2,9 @@ package dynamodb
import (
"bytes"
- "encoding/json"
"github.com/bmizerany/aws4"
"io/ioutil"
"net/http"
- "reflect"
- "strconv"
"time"
)
@@ -30,15 +27,6 @@ var (
APSouthEast1 *Region = &Region{"ap-southeast-1", "dynamodb.ap-southeast-1.amazonaws.com"}
)
-type RequestError struct {
- Status string
- Message string
-}
-
-func (r RequestError) Error() string {
- return "Status: " + r.Status + ", Message: " + r.Message
-}
-
type Table struct {
name string
region *Region
@@ -52,94 +40,8 @@ func NewTable(name string, region *Region, awsAccessKeyId string, awsSecretAcces
return &Table{name, region, k, s}
}
-type PutRequestItem struct {
- Value interface{}
-}
-
-type PutItemRequest struct {
- TableName string
- Item PutRequestItem
-}
-
-func fieldToDynamoString(v reflect.Value) (typeId string, value string, err error) {
- if v.Kind() == reflect.Interface || v.Kind() == reflect.Ptr {
- v = v.Elem()
- }
-
- switch v.Kind() {
-
- case reflect.String:
- return "S", v.Interface().(string), nil
-
- case reflect.Int:
- return "N", strconv.FormatInt(int64(v.Interface().(int)), 10), nil
- case reflect.Int8:
- return "N", strconv.FormatInt(int64(v.Interface().(int8)), 10), nil
- case reflect.Int16:
- return "N", strconv.FormatInt(int64(v.Interface().(int16)), 10), nil
- case reflect.Int32:
- return "N", strconv.FormatInt(int64(v.Interface().(int32)), 10), nil
- case reflect.Int64:
- return "N", strconv.FormatInt(v.Interface().(int64), 10), nil
-
- case reflect.Uint:
- return "N", strconv.FormatUint(uint64(v.Interface().(uint)), 10), nil
- case reflect.Uint8:
- return "N", strconv.FormatUint(uint64(v.Interface().(uint8)), 10), nil
- case reflect.Uint16:
- return "N", strconv.FormatUint(uint64(v.Interface().(uint16)), 10), nil
- case reflect.Uint32:
- return "N", strconv.FormatUint(uint64(v.Interface().(uint32)), 10), nil
- case reflect.Uint64:
- return "N", strconv.FormatUint(v.Interface().(uint64), 10), nil
-
- case reflect.Float32:
- return "N", strconv.FormatFloat(float64(v.Interface().(float32)), 'f', -1, 32), nil
- case reflect.Float64:
- return "N", strconv.FormatFloat(v.Interface().(float64), 'f', -1, 64), nil
-
- }
-
- return "", "", &json.MarshalerError{Type: v.Type()}
-}
-
-func (i PutRequestItem) MarshalJSON() ([]byte, error) {
- var out bytes.Buffer
-
- v := reflect.ValueOf(i.Value)
- for v.Kind() == reflect.Interface || v.Kind() == reflect.Ptr {
- v = v.Elem()
- }
-
- t := v.Type()
-
- out.WriteString("{")
- for i := 0; i < v.NumField(); i++ {
- f := v.Field(i)
- out.WriteString("\"" + t.Field(i).Name + "\":")
-
- typeId, fieldVal, err := fieldToDynamoString(f)
- if err != nil {
- return []byte(""), err
- }
-
- out.WriteString("{")
- out.WriteString("\"" + typeId + "\":")
- out.WriteString("\"" + fieldVal + "\"")
- out.WriteString("}")
-
- if i < v.NumField()-1 {
- out.WriteString(",")
- }
- }
- out.WriteString("}")
-
- return out.Bytes(), nil
-}
-
func (t *Table) PutItem(item interface{}) error {
- data := PutItemRequest{TableName: t.name, Item: PutRequestItem{&item}}
- body, err := json.Marshal(data)
+ body, err := t.putItemRequestBody(item)
if err != nil {
return err
}
@@ -175,3 +77,12 @@ func (t *Table) PutItem(item interface{}) error {
return nil
}
+
+type RequestError struct {
+ Status string
+ Message string
+}
+
+func (r RequestError) Error() string {
+ return "Status: " + r.Status + ", Message: " + r.Message
+}
View
@@ -12,7 +12,7 @@ type PutRequestWithString struct {
func TestPutRequestStringSerialization(t *testing.T) {
value := &PutRequestWithString{"some text"}
- item := PutRequestItem{value}
+ item := putRequestItem{value}
data, err := json.Marshal(&item)
if err != nil {
t.Fatal(err)
@@ -37,7 +37,7 @@ type PutRequestWithInt struct {
func TestPutRequestIntSerialization(t *testing.T) {
value := &PutRequestWithInt{12, 34, 56, 78, 90, 11, 21, 31, 41, 51}
- item := PutRequestItem{value}
+ item := putRequestItem{value}
data, err := json.Marshal(&item)
if err != nil {
t.Fatal(err)
@@ -57,7 +57,7 @@ type PutRequestWithFloat struct {
func TestPutRequestFloatSerialization(t *testing.T) {
value := &PutRequestWithFloat{123.4567, 987654321.123456789}
- item := PutRequestItem{value}
+ item := putRequestItem{value}
data, err := json.Marshal(&item)
if err != nil {
t.Fatal(err)
View
98 json.go
@@ -0,0 +1,98 @@
+package dynamodb
+
+import (
+ "bytes"
+ "encoding/json"
+ "reflect"
+ "strconv"
+)
+
+type putItemRequest struct {
+ TableName string
+ Item putRequestItem
+}
+
+func (t *Table) putItemRequestBody(item interface{}) ([]byte, error) {
+ data := putItemRequest{TableName: t.name, Item: putRequestItem{&item}}
+ return json.Marshal(data)
+}
+
+type putRequestItem struct {
+ Value interface{}
+}
+
+func (i putRequestItem) MarshalJSON() ([]byte, error) {
+ var out bytes.Buffer
+
+ v := reflect.ValueOf(i.Value)
+ for v.Kind() == reflect.Interface || v.Kind() == reflect.Ptr {
+ v = v.Elem()
+ }
+
+ t := v.Type()
+
+ out.WriteString("{")
+ for i := 0; i < v.NumField(); i++ {
+ f := v.Field(i)
+ out.WriteString("\"" + t.Field(i).Name + "\":")
+
+ typeId, fieldVal, err := fieldToDynamoString(f)
+ if err != nil {
+ return []byte(""), err
+ }
+
+ out.WriteString("{")
+ out.WriteString("\"" + typeId + "\":")
+ out.WriteString("\"" + fieldVal + "\"")
+ out.WriteString("}")
+
+ if i < v.NumField()-1 {
+ out.WriteString(",")
+ }
+ }
+ out.WriteString("}")
+
+ return out.Bytes(), nil
+}
+
+func fieldToDynamoString(v reflect.Value) (typeId string, value string, err error) {
+ if v.Kind() == reflect.Interface || v.Kind() == reflect.Ptr {
+ v = v.Elem()
+ }
+
+ switch v.Kind() {
+
+ case reflect.String:
+ return "S", v.Interface().(string), nil
+
+ case reflect.Int:
+ return "N", strconv.FormatInt(int64(v.Interface().(int)), 10), nil
+ case reflect.Int8:
+ return "N", strconv.FormatInt(int64(v.Interface().(int8)), 10), nil
+ case reflect.Int16:
+ return "N", strconv.FormatInt(int64(v.Interface().(int16)), 10), nil
+ case reflect.Int32:
+ return "N", strconv.FormatInt(int64(v.Interface().(int32)), 10), nil
+ case reflect.Int64:
+ return "N", strconv.FormatInt(v.Interface().(int64), 10), nil
+
+ case reflect.Uint:
+ return "N", strconv.FormatUint(uint64(v.Interface().(uint)), 10), nil
+ case reflect.Uint8:
+ return "N", strconv.FormatUint(uint64(v.Interface().(uint8)), 10), nil
+ case reflect.Uint16:
+ return "N", strconv.FormatUint(uint64(v.Interface().(uint16)), 10), nil
+ case reflect.Uint32:
+ return "N", strconv.FormatUint(uint64(v.Interface().(uint32)), 10), nil
+ case reflect.Uint64:
+ return "N", strconv.FormatUint(v.Interface().(uint64), 10), nil
+
+ case reflect.Float32:
+ return "N", strconv.FormatFloat(float64(v.Interface().(float32)), 'f', -1, 32), nil
+ case reflect.Float64:
+ return "N", strconv.FormatFloat(v.Interface().(float64), 'f', -1, 64), nil
+
+ }
+
+ return "", "", &json.MarshalerError{Type: v.Type()}
+}

0 comments on commit 3cf4e21

Please sign in to comment.