本文整理汇总了Golang中github.com/cloudwan/gohan/schema.Resource.Data方法的典型用法代码示例。如果您正苦于以下问题:Golang Resource.Data方法的具体用法?Golang Resource.Data怎么用?Golang Resource.Data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cloudwan/gohan/schema.Resource
的用法示例。
在下文中一共展示了Resource.Data方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CreateResourceInTransaction
//CreateResourceInTransaction craete db resource model in transaction
func CreateResourceInTransaction(context middleware.Context, resource *schema.Resource) error {
resourceSchema := resource.Schema()
mainTransaction := context["transaction"].(transaction.Transaction)
environmentManager := extension.GetManager()
environment, ok := environmentManager.GetEnvironment(resourceSchema.ID)
if !ok {
return fmt.Errorf("No environment for schema")
}
if err := extension.HandleEvent(context, environment, "pre_create_in_transaction"); err != nil {
return err
}
if err := mainTransaction.Create(resource); err != nil {
log.Debug("%s transaction error", err)
return ResourceError{
err,
fmt.Sprintf("Failed to store data in database: %v", err),
CreateFailed}
}
response := map[string]interface{}{}
response[resourceSchema.Singular] = resource.Data()
context["response"] = response
if err := extension.HandleEvent(context, environment, "post_create_in_transaction"); err != nil {
return err
}
return nil
}
示例2: Create
//Create create resource in the db
func (tx *Transaction) Create(resource *schema.Resource) error {
var cols []string
var values []interface{}
db := tx.db
s := resource.Schema()
data := resource.Data()
q := sq.Insert(quote(s.GetDbTableName()))
for _, attr := range s.Properties {
//TODO(nati) support optional value
if _, ok := data[attr.ID]; ok {
handler := db.handler(&attr)
cols = append(cols, quote(attr.ID))
encoded, err := handler.encode(&attr, data[attr.ID])
if err != nil {
return fmt.Errorf("SQL Create encoding error: %s", err)
}
values = append(values, encoded)
}
}
q = q.Columns(cols...).Values(values...)
sql, args, err := q.ToSql()
if err != nil {
return err
}
return tx.Exec(sql, args...)
}
示例3: Update
//Update update resource in the db
func (tx *Transaction) Update(resource *schema.Resource) error {
s := resource.Schema()
data := resource.Data()
db := tx.db
q := sq.Update(quote(s.GetDbTableName()))
for _, attr := range s.Properties {
//TODO(nati) support optional value
if _, ok := data[attr.ID]; ok {
handler := db.handler(&attr)
encoded, err := handler.encode(&attr, data[attr.ID])
if err != nil {
return fmt.Errorf("SQL Update encoding error: %s", err)
}
q = q.Set(quote(attr.ID), encoded)
}
}
if s.Parent != "" {
q.Set(s.ParentSchemaPropertyID(), resource.ParentID())
}
q = q.Where(sq.Eq{"id": resource.ID()})
sql, args, err := q.ToSql()
if err != nil {
return err
}
return tx.Exec(sql, args...)
}
示例4: Create
//Create create resource in the db
func (tx *Transaction) Create(resource *schema.Resource) error {
db := tx.db
db.load()
s := resource.Schema()
data := resource.Data()
table := db.getTable(s)
db.data[s.GetDbTableName()] = append(table, data)
db.write()
return nil
}
示例5: Update
//Update update resource in the db
func (tx *Transaction) Update(resource *schema.Resource) error {
db := tx.db
db.load()
s := resource.Schema()
data := resource.Data()
table := db.getTable(s)
for _, rawDataInDB := range table {
dataInDB := rawDataInDB.(map[string]interface{})
if dataInDB["id"] == resource.ID() {
for key, value := range data {
dataInDB[key] = value
}
}
}
db.write()
return nil
}
示例6: updateQuery
func (tx *Transaction) updateQuery(resource *schema.Resource) (sq.UpdateBuilder, error) {
s := resource.Schema()
db := tx.db
data := resource.Data()
q := sq.Update(quote(s.GetDbTableName()))
for _, attr := range s.Properties {
//TODO(nati) support optional value
if _, ok := data[attr.ID]; ok {
handler := db.handler(&attr)
encoded, err := handler.encode(&attr, data[attr.ID])
if err != nil {
return q, fmt.Errorf("SQL Update encoding error: %s", err)
}
q = q.Set(quote(attr.ID), encoded)
}
}
if s.Parent != "" {
q = q.Set(s.ParentSchemaPropertyID(), resource.ParentID())
}
return q, nil
}
示例7: GetSingleResourceInTransaction
//GetSingleResourceInTransaction get resource in single transaction
func GetSingleResourceInTransaction(context middleware.Context, resourceSchema *schema.Schema, resourceID string, tenantIDs []string) (err error) {
mainTransaction := context["transaction"].(transaction.Transaction)
auth := context["auth"].(schema.Authorization)
environmentManager := extension.GetManager()
environment, ok := environmentManager.GetEnvironment(resourceSchema.ID)
if !ok {
return fmt.Errorf("no environment for schema")
}
if err := handleEvent(context, environment, "pre_show_in_transaction"); err != nil {
return err
}
if rawResponse, ok := context["response"]; ok {
if _, ok := rawResponse.(map[string]interface{}); ok {
return nil
}
return fmt.Errorf("extension returned invalid JSON: %v", rawResponse)
}
var object *schema.Resource
if resourceSchema.ID == "schema" {
manager := schema.GetManager()
requestedSchema, _ := manager.Schema(resourceID)
object, err = GetSchema(requestedSchema, auth)
} else {
object, err = mainTransaction.Fetch(resourceSchema, resourceID, tenantIDs)
}
if err != nil || object == nil {
return ResourceError{err, "", NotFound}
}
response := map[string]interface{}{}
response[resourceSchema.Singular] = object.Data()
context["response"] = response
if err := handleEvent(context, environment, "post_show_in_transaction"); err != nil {
return err
}
return
}
示例8:
})
It("Returns the error with invalid filter", func() {
filter := map[string]interface{}{
"bad_filter": []string{"red"},
}
_, _, err := tx.List(networkSchema, filter, nil)
Expect(err).To(HaveOccurred())
})
It("Shows related resources", func() {
list, num, err := tx.List(serverSchema, nil, nil)
Expect(err).ToNot(HaveOccurred())
Expect(num).To(Equal(uint64(1)))
Expect(list).To(HaveLen(1))
Expect(list[0].Data()).To(HaveKeyWithValue("network", HaveKeyWithValue("name", networkResource1.Data()["name"])))
Expect(tx.Commit()).To(Succeed())
})
It("Fetches an existing resource", func() {
networkResourceFetched, err := tx.Fetch(networkSchema, transaction.IDFilter(networkResource1.ID()))
Expect(err).ToNot(HaveOccurred())
Expect(networkResourceFetched).To(util.MatchAsJSON(networkResource1))
Expect(tx.Commit()).To(Succeed())
})
It("Updates the resource properly", func() {
By("Not allowing to update some fields")
Expect(networkResource1.Update(map[string]interface{}{"id": "new_id"})).ToNot(Succeed())
By("Updating other fields")