Skip to content

Commit

Permalink
fixed issue 1 and merged in the changes
Browse files Browse the repository at this point in the history
  • Loading branch information
matryer committed Jan 27, 2012
1 parent c20fb7b commit febbc3d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
38 changes: 25 additions & 13 deletions gaerecords/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (r *Record) Save(c chan<- datastore.Property) os.Error {

for k, v := range r.Fields() {

if reflect.TypeOf(v).Kind() == reflect.Array || reflect.TypeOf(v).Kind() == reflect.Slice {
if (reflect.TypeOf(v) != reflect.TypeOf(([]byte)(nil))) && (reflect.TypeOf(v).Kind() == reflect.Array || reflect.TypeOf(v).Kind() == reflect.Slice) {

// multiple values - iterate over each value
// and add them as seperate properties
Expand All @@ -178,26 +178,17 @@ func (r *Record) Save(c chan<- datastore.Property) os.Error {

for i := 0; i < l; i++ {

thisVal := value.Index(i)
thisVal := value.Index(i).Interface()

// create the property
c <- datastore.Property{
Name: k,
Value: thisVal.Interface(),
Multiple: true,
}
c <- fieldToProperty(k, thisVal, true)

}

} else {

// single value
// create the property
c <- datastore.Property{
Name: k,
Value: v,
Multiple: false,
}
c <- fieldToProperty(k, v, false)

}

Expand All @@ -210,6 +201,27 @@ func (r *Record) Save(c chan<- datastore.Property) os.Error {
return nil
}

// fieldToProperty turns a field and returns a datastore.Proprty
func fieldToProperty(key string, value interface{}, multiple bool) datastore.Property {

var noindex bool = false

// []byte fields cannot be indexed
if reflect.TypeOf(value) == reflect.TypeOf(([]byte)(nil)) {
noindex = true
}

// single value
// create the property
return datastore.Property{
Name: key,
Value: value,
Multiple: multiple,
NoIndex: noindex,
}

}

// configureRecord (Internal) Configures a Record after it has been found or created using means other than
// model.New() or NewRecord(model).
func (r *Record) configureRecord(model *Model, key *datastore.Key) *Record {
Expand Down
27 changes: 27 additions & 0 deletions gaerecords/record_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ func TestGetAndSetBlobKeyField(t *testing.T) {

}

func TestGetAndSetBytes(t *testing.T) {

people := CreateTestModel()
person := people.New()

var bytes []byte = []byte("This is a test value")

assertEqual(t, person, person.SetBytes("field", bytes))
assertEqual(t, string(bytes), string(person.Fields()["field"].([]byte)))
assertEqual(t, string(bytes), string(person.GetBytes("field")))

err := person.Put()
if err != nil {
t.Errorf("Failed to Put []byte: %v", err)
}

}

func TestDifferentNumbericalValueTypes(t *testing.T) {

people := CreateTestModel()
Expand Down Expand Up @@ -279,6 +297,11 @@ func TestSetMultiple_StronglyTypedVarients(t *testing.T) {
assertEqual(t, person, person.SetMultipleTimes("many-times", []datastore.Time{time1, time2, time3}))
assertEqual(t, person, person.SetMultipleBlobKeys("many-blob-keys", []appengine.BlobKey{blobKey1, blobKey2, blobKey3}))

bytes1 := []byte("A")
bytes2 := []byte("B")
bytes3 := []byte("C")
assertEqual(t, person, person.SetMultipleBytes("manybytes", [][]byte{bytes1, bytes2, bytes3}))

err := person.Put()

if err != nil {
Expand Down Expand Up @@ -321,6 +344,10 @@ func TestSetMultiple_StronglyTypedVarients(t *testing.T) {
assertEqual(t, blobKey2, loaded.GetMultiple("many-blob-keys")[1])
assertEqual(t, blobKey3, loaded.GetMultiple("many-blob-keys")[2])

assertEqual(t, bytes1[0], loaded.GetMultiple("manybytes")[0].([]byte)[0])
assertEqual(t, bytes2[0], loaded.GetMultiple("manybytes")[1].([]byte)[0])
assertEqual(t, bytes3[0], loaded.GetMultiple("manybytes")[2].([]byte)[0])

}

}

0 comments on commit febbc3d

Please sign in to comment.