當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Resource.Get方法代碼示例

本文整理匯總了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
}
開發者ID:gitter-badger,項目名稱:gohan,代碼行數:43,代碼來源:sync.go

示例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
}
開發者ID:saeki-masaki,項目名稱:gohan,代碼行數:53,代碼來源:sync.go

示例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
}
開發者ID:vozhyk-,項目名稱:gohan,代碼行數:80,代碼來源:sync.go

示例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"))
//.........這裏部分代碼省略.........
開發者ID:cloudwan,項目名稱:gohan,代碼行數:101,代碼來源:sync.go


注:本文中的github.com/cloudwan/gohan/schema.Resource.Get方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。