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


Golang Resource.Schema方法代碼示例

本文整理匯總了Golang中github.com/cloudwan/gohan/schema.Resource.Schema方法的典型用法代碼示例。如果您正苦於以下問題:Golang Resource.Schema方法的具體用法?Golang Resource.Schema怎麽用?Golang Resource.Schema使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/cloudwan/gohan/schema.Resource的用法示例。


在下文中一共展示了Resource.Schema方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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
}
開發者ID:cloudwan,項目名稱:gohan,代碼行數:30,代碼來源:resource_management.go

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

示例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...)
}
開發者ID:gitter-badger,項目名稱:gohan,代碼行數:27,代碼來源:sql.go

示例4: logEvent

func (tl *transactionEventLogger) logEvent(eventType string, resource *schema.Resource, version int64) error {
	schemaManager := schema.GetManager()
	eventSchema, ok := schemaManager.Schema("event")
	if !ok {
		return fmt.Errorf("event schema not found")
	}

	if resource.Schema().Metadata["nosync"] == true {
		log.Debug("skipping event logging for schema: %s", resource.Schema().ID)
		return nil
	}
	body, err := resource.JSONString()
	if err != nil {
		return fmt.Errorf("Error during event resource deserialisation: %s", err.Error())
	}
	eventResource, err := schema.NewResource(eventSchema, map[string]interface{}{
		"type":      eventType,
		"path":      resource.Path(),
		"version":   version,
		"body":      body,
		"timestamp": int64(time.Now().Unix()),
	})
	tl.eventLogged = true
	return tl.Transaction.Create(eventResource)
}
開發者ID:vozhyk-,項目名稱:gohan,代碼行數:25,代碼來源:sync.go

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

示例6: Update

func (tl *transactionEventLogger) Update(resource *schema.Resource) error {
	err := tl.Transaction.Update(resource)
	if err != nil {
		return err
	}
	if !resource.Schema().StateVersioning() {
		return tl.logEvent("update", resource, 0)
	}
	state, err := tl.StateFetch(resource.Schema(), transaction.IDFilter(resource.ID()))
	if err != nil {
		return err
	}
	return tl.logEvent("update", resource, state.ConfigVersion)
}
開發者ID:cloudwan,項目名稱:gohan,代碼行數:14,代碼來源:transaction_commit_informer.go

示例7: Update

//Update update resource in the db
func (tx *Transaction) Update(resource *schema.Resource) error {
	q, err := tx.updateQuery(resource)
	if err != nil {
		return err
	}
	sql, args, err := q.ToSql()
	if err != nil {
		return err
	}
	if resource.Schema().StateVersioning() {
		sql += ", `" + configVersionColumnName + "` = `" + configVersionColumnName + "` + 1"
	}
	sql += " WHERE id = ?"
	args = append(args, resource.ID())
	return tx.Exec(sql, args...)
}
開發者ID:vozhyk-,項目名稱:gohan,代碼行數:17,代碼來源:sql.go

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

示例9: StateUpdate

//StateUpdate update resource state
func (tx *Transaction) StateUpdate(resource *schema.Resource, state *transaction.ResourceState) error {
	q, err := tx.updateQuery(resource)
	if err != nil {
		return err
	}
	if resource.Schema().StateVersioning() && state != nil {
		q = q.Set(quote(stateVersionColumnName), state.StateVersion)
		q = q.Set(quote(stateErrorColumnName), state.Error)
		q = q.Set(quote(stateColumnName), state.State)
		q = q.Set(quote(stateMonitoringColumnName), state.Monitoring)
	}
	q = q.Where(sq.Eq{"id": resource.ID()})
	sql, args, err := q.ToSql()
	if err != nil {
		return err
	}
	return tx.Exec(sql, args...)
}
開發者ID:vozhyk-,項目名稱:gohan,代碼行數:19,代碼來源:sql.go

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

示例11: makePluralKey

func makePluralKey(resource *schema.Resource) string {
	s := resource.Schema()
	key := s.Prefix + "/" + s.Plural
	return key
}
開發者ID:gitter-badger,項目名稱:gohan,代碼行數:5,代碼來源:db_test.go


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