// Store stores the document in specified database.
// obj: a Document-embedded struct value, its id and rev will be updated after stored,
// so caller must pass a pointer value.
Store stores the document in specified database.
obj: a Document-embedded struct value, its id and rev will be updated after stored,
so caller must pass a pointer value.
// Store stores the document in specified database . // obj : a Document - embedded struct value its id and rev will be updated after stored // so caller must pass a pointer value .
func Store(db *Database, obj interface{}) error {
	ptrValue := reflect.ValueOf(obj)
	if ptrValue.Kind() != reflect.Ptr || ptrValue.Elem().Kind() != reflect.Struct {
		return ErrNotStruct
	}
	if ptrValue.Elem().FieldByName("Document") == zero {
		return ErrNotDocumentEmbedded
	}
	jsonIDField := ptrValue.Elem().FieldByName("ID")
	getIDMethod := ptrValue.MethodByName("GetID")
	idStr := getIDMethod.Call([]reflect.Value{})[0].Interface().(string)
	if idStr != "" {
		jsonIDField.SetString(idStr)
	}
	jsonRevField := ptrValue.Elem().FieldByName("Rev")
	getRevMethod := ptrValue.MethodByName("GetRev")
	revStr := getRevMethod.Call([]reflect.Value{})[0].Interface().(string)
	if revStr != "" {
		jsonRevField.SetString(revStr)
	}
	doc, err := ToJSONCompatibleMap(ptrValue.Elem().Interface())
	if err != nil {
		return err
	}
	id, rev, err := db.Save(doc, nil)
	if err != nil {
		return err
	}
	setIDMethod := ptrValue.MethodByName("SetID")
	setRevMethod := ptrValue.MethodByName("SetRev")
	if idStr == "" {
		setIDMethod.Call([]reflect.Value{reflect.ValueOf(id)})
	}
	setRevMethod.Call([]reflect.Value{reflect.ValueOf(rev)})
	jsonRevField.SetString(rev)
	return nil
}
func Store ( db * Database , obj interface { } ) error { ptrValue := reflect . ValueOf ( obj ) if ptrValue . Kind ( ) != reflect . Ptr || ptrValue . Elem ( ) . Kind ( ) != reflect . Struct { return ErrNotStruct } if ptrValue . Elem ( ) . FieldByName ( " " ) == zero { return ErrNotDocumentEmbedded } jsonIDField := ptrValue . Elem ( ) . FieldByName ( " " ) getIDMethod := ptrValue . MethodByName ( " " ) idStr := getIDMethod . Call ( [ ] reflect . Value { } ) [ 0 ] . Interface ( ) . ( string ) if idStr != " " { jsonIDField . SetString ( idStr ) } jsonRevField := ptrValue . Elem ( ) . FieldByName ( " " ) getRevMethod := ptrValue . MethodByName ( " " ) revStr := getRevMethod . Call ( [ ] reflect . Value { } ) [ 0 ] . Interface ( ) . ( string ) if revStr != " " { jsonRevField . SetString ( revStr ) } doc , err := ToJSONCompatibleMap ( ptrValue . Elem ( ) . Interface ( ) ) if err != nil { return err } id , rev , err := db . Save ( doc , nil ) if err != nil { return err } setIDMethod := ptrValue . MethodByName ( " " ) setRevMethod := ptrValue . MethodByName ( " " ) if idStr == " " { setIDMethod . Call ( [ ] reflect . Value { reflect . ValueOf ( id ) } ) } setRevMethod . Call ( [ ] reflect . Value { reflect . ValueOf ( rev ) } ) jsonRevField . SetString ( rev ) return nil }
Store stores the document in specified database.
Store stores the document in specified database .