当前位置: 首页>>代码示例>>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;未经允许,请勿转载。