本文整理汇总了Golang中github.com/cloudwan/gohan/schema.Resource.Get方法的典型用法代码示例。如果您正苦于以下问题:Golang Resource.Get方法的具体用法?Golang Resource.Get怎么用?Golang Resource.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cloudwan/gohan/schema.Resource
的用法示例。
在下文中一共展示了Resource.Get方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: syncEvent
func (server *Server) syncEvent(resource *schema.Resource) error {
schemaManager := schema.GetManager()
eventSchema, _ := schemaManager.Schema("event")
tx, err := server.db.Begin()
if err != nil {
return err
}
defer tx.Close()
eventType := resource.Get("type").(string)
path := resource.Get("path").(string)
body := resource.Get("body").(string)
log.Debug("event %s", eventType)
if eventType == "create" || eventType == "update" {
log.Debug("set %s on sync", path)
err = server.sync.Update(path, body)
if err != nil {
log.Error(fmt.Sprintf("%s on sync", err))
return err
}
} else if eventType == "delete" {
log.Debug("delete %s", path)
err = server.sync.Delete(path)
if err != nil {
log.Error(fmt.Sprintf("Delete from sync failed %s", err))
return err
}
}
log.Debug("delete event %d", resource.Get("id"))
id := resource.Get("id")
err = tx.Delete(eventSchema, id)
if err != nil {
log.Error(fmt.Sprintf("delete failed: %s", err))
return err
}
err = tx.Commit()
if err != nil {
log.Error(fmt.Sprintf("commit failed: %s", err))
return err
}
return nil
}
示例2: syncEvent
func (server *Server) syncEvent(resource *schema.Resource) error {
schemaManager := schema.GetManager()
eventSchema, _ := schemaManager.Schema("event")
tx, err := server.db.Begin()
if err != nil {
return err
}
defer tx.Close()
eventType := resource.Get("type").(string)
path := resource.Get("path").(string)
path = configPrefix + path
body := resource.Get("body").(string)
version, _ := resource.Get("version").(uint64)
log.Debug("event %s", eventType)
if eventType == "create" || eventType == "update" {
log.Debug("set %s on sync", path)
content, err := json.Marshal(map[string]interface{}{
"body": body,
"version": version,
})
if err != nil {
log.Error(fmt.Sprintf("When marshalling sync object: %s", err))
return err
}
err = server.sync.Update(path, string(content))
if err != nil {
log.Error(fmt.Sprintf("%s on sync", err))
return err
}
} else if eventType == "delete" {
log.Debug("delete %s", path)
err = server.sync.Delete(path)
if err != nil {
log.Error(fmt.Sprintf("Delete from sync failed %s", err))
return err
}
}
log.Debug("delete event %d", resource.Get("id"))
id := resource.Get("id")
err = tx.Delete(eventSchema, id)
if err != nil {
log.Error(fmt.Sprintf("delete failed: %s", err))
return err
}
err = tx.Commit()
if err != nil {
log.Error(fmt.Sprintf("commit failed: %s", err))
return err
}
return nil
}
示例3: syncEvent
func (server *Server) syncEvent(resource *schema.Resource) error {
schemaManager := schema.GetManager()
eventSchema, _ := schemaManager.Schema("event")
tx, err := server.db.Begin()
if err != nil {
return err
}
defer tx.Close()
eventType := resource.Get("type").(string)
resourcePath := resource.Get("path").(string)
body := resource.Get("body").(string)
path := generatePath(resourcePath, body)
version, ok := resource.Get("version").(int)
if !ok {
log.Debug("cannot cast version value in int for %s", path)
}
log.Debug("event %s", eventType)
if eventType == "create" || eventType == "update" {
log.Debug("set %s on sync", path)
content, err := json.Marshal(map[string]interface{}{
"body": body,
"version": version,
})
if err != nil {
log.Error(fmt.Sprintf("When marshalling sync object: %s", err))
return err
}
err = server.sync.Update(path, string(content))
if err != nil {
log.Error(fmt.Sprintf("%s on sync", err))
return err
}
} else if eventType == "delete" {
log.Debug("delete %s", resourcePath)
deletePath := resourcePath
resourceSchema := schema.GetSchemaByURLPath(resourcePath)
if _, ok := resourceSchema.SyncKeyTemplate(); ok {
var data map[string]interface{}
err := json.Unmarshal(([]byte)(body), &data)
deletePath, err = resourceSchema.GenerateCustomPath(data)
if err != nil {
log.Error(fmt.Sprintf("Delete from sync failed %s - generating of custom path failed", err))
return err
}
}
log.Debug("deleting %s", statePrefix+deletePath)
err = server.sync.Delete(statePrefix + deletePath)
if err != nil {
log.Error(fmt.Sprintf("Delete from sync failed %s", err))
}
log.Debug("deleting %s", monitoringPrefix+deletePath)
err = server.sync.Delete(monitoringPrefix + deletePath)
if err != nil {
log.Error(fmt.Sprintf("Delete from sync failed %s", err))
}
log.Debug("deleting %s", resourcePath)
err = server.sync.Delete(path)
if err != nil {
log.Error(fmt.Sprintf("Delete from sync failed %s", err))
return err
}
}
log.Debug("delete event %d", resource.Get("id"))
id := resource.Get("id")
err = tx.Delete(eventSchema, id)
if err != nil {
log.Error(fmt.Sprintf("delete failed: %s", err))
return err
}
err = tx.Commit()
if err != nil {
log.Error(fmt.Sprintf("commit failed: %s", err))
return err
}
return nil
}
示例4: syncEvent
func (server *Server) syncEvent(resource *schema.Resource) error {
schemaManager := schema.GetManager()
eventSchema, _ := schemaManager.Schema("event")
tx, err := server.db.Begin()
if err != nil {
return err
}
defer tx.Close()
eventType := resource.Get("type").(string)
resourcePath := resource.Get("path").(string)
body := resource.Get("body").(string)
syncPlain := resource.Get("sync_plain").(bool)
syncProperty := resource.Get("sync_property").(string)
path := generatePath(resourcePath, body)
version, ok := resource.Get("version").(int)
if !ok {
log.Debug("cannot cast version value in int for %s", path)
}
log.Debug("event %s", eventType)
if eventType == "create" || eventType == "update" {
log.Debug("set %s on sync", path)
content := body
var data map[string]interface{}
if syncProperty != "" {
err = json.Unmarshal(([]byte)(body), &data)
if err != nil {
log.Error(fmt.Sprintf("failed to unmarshal body on sync: %s", err))
return err
}
target, ok := data[syncProperty]
if !ok {
return fmt.Errorf("could not find property `%s`", syncProperty)
}
jsonData, err := json.Marshal(target)
if err != nil {
return err
}
content = string(jsonData)
}
if syncPlain {
var target interface{}
json.Unmarshal([]byte(content), &target)
switch target.(type) {
case string:
content = fmt.Sprintf("%v", target)
}
} else {
data, err := json.Marshal(map[string]interface{}{
"body": content,
"version": version,
})
if err != nil {
log.Error(fmt.Sprintf("When marshalling sync object: %s", err))
return err
}
content = string(data)
}
err = server.sync.Update(path, content)
if err != nil {
log.Error(fmt.Sprintf("%s on sync", err))
return err
}
} else if eventType == "delete" {
log.Debug("delete %s", resourcePath)
deletePath := resourcePath
resourceSchema := schema.GetSchemaByURLPath(resourcePath)
if _, ok := resourceSchema.SyncKeyTemplate(); ok {
var data map[string]interface{}
json.Unmarshal(([]byte)(body), &data)
deletePath, err = resourceSchema.GenerateCustomPath(data)
if err != nil {
log.Error(fmt.Sprintf("Delete from sync failed %s - generating of custom path failed", err))
return err
}
}
log.Debug("deleting %s", statePrefix+deletePath)
err = server.sync.Delete(statePrefix + deletePath)
if err != nil {
log.Error(fmt.Sprintf("Delete from sync failed %s", err))
}
log.Debug("deleting %s", monitoringPrefix+deletePath)
err = server.sync.Delete(monitoringPrefix + deletePath)
if err != nil {
log.Error(fmt.Sprintf("Delete from sync failed %s", err))
}
log.Debug("deleting %s", resourcePath)
err = server.sync.Delete(path)
if err != nil {
log.Error(fmt.Sprintf("Delete from sync failed %s", err))
return err
}
}
log.Debug("delete event %d", resource.Get("id"))
//.........这里部分代码省略.........