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


Golang go-appkit.Model類代碼示例

本文整理匯總了Golang中github.com/app-kit/go-appkit.Model的典型用法代碼示例。如果您正苦於以下問題:Golang Model類的具體用法?Golang Model怎麽用?Golang Model使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Model類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: AllowUpdate

func (hooks UserResourceHooks) AllowUpdate(res kit.Resource, obj kit.Model, old kit.Model, user kit.User) bool {
	if user == nil {
		return false
	}
	if user.HasRole("admin") || user.HasPermission("users.update") {
		return true
	}
	return obj.GetId() == user.GetId()
}
開發者ID:app-kit,項目名稱:go-appkit,代碼行數:9,代碼來源:resources.go

示例2: update

func (res *Resource) update(obj kit.Model, user kit.User, partial bool) apperror.Error {
	if hook, ok := res.hooks.(UpdateHook); ok {
		return hook.Update(res, obj, user)
	}

	oldObj, err := res.FindOne(obj.GetId())
	if err != nil {
		return err
	} else if oldObj == nil {
		return apperror.New("not_found")
	}

	if allowUpdate, ok := res.hooks.(AllowUpdateHook); ok {
		if !allowUpdate.AllowUpdate(res, obj, oldObj, user) {
			return apperror.New("permission_denied")
		}
	}

	if beforeUpdate, ok := res.hooks.(BeforeUpdateHook); ok {
		if err := beforeUpdate.BeforeUpdate(res, obj, oldObj, user); err != nil {
			return err
		}
	}

	if partial {
		rOld := reflector.Reflect(oldObj).MustStruct()
		rNew := reflector.Reflect(oldObj).MustStruct()

		for fieldName, _ := range res.modelInfo.Attributes() {
			val := rNew.Field(fieldName)
			if !val.IsZero() {
				rOld.Field(fieldName).Set(val)
			}
		}
		for fieldName, _ := range res.modelInfo.Relations() {
			val := rNew.Field(fieldName)
			if !val.IsZero() {
				rOld.Field(fieldName).Set(val)
			}
		}

		obj = oldObj
	}

	if err := res.backend.Update(obj); err != nil {
		return err
	}

	if afterUpdate, ok := res.hooks.(AfterUpdateHook); ok {
		if err := afterUpdate.AfterUpdate(res, obj, oldObj, user); err != nil {
			return err
		}
	}

	return nil
}
開發者ID:app-kit,項目名稱:go-appkit,代碼行數:56,代碼來源:resource.go

示例3: Create

func (s *Service) Create(m kit.Model, user kit.User) apperror.Error {
	res := s.resources[m.Collection()]
	if res == nil {
		return &apperror.Err{
			Code:    "unknown_resource",
			Message: fmt.Sprintf("The resource %v was not registered with service", m.Collection()),
		}
	}

	return res.Create(m, user)
}
開發者ID:app-kit,項目名稱:go-appkit,代碼行數:11,代碼來源:service.go

示例4: SerializeModel

// SerializeModel converts a model into the target format.
func (s *Serializer) SerializeModel(model kit.Model) (interface{}, []interface{}, apperror.Error) {
	// Find backend.
	backend := s.findBackend(model.Collection())
	if backend == nil {
		return nil, nil, apperror.New("unknown_collection", fmt.Sprintf("Can't serialize model of unknown collection %v", model.Collection()))
	}

	// Serialize model.
	m, extra, err := SerializeModel(backend, model)
	if err != nil {
		return nil, nil, err
	}

	// Convert extra slice to interface slice.
	var rawExtra []interface{}
	for _, item := range extra {
		rawExtra = append(rawExtra, item)
	}

	return m, rawExtra, nil
}
開發者ID:app-kit,項目名稱:go-appkit,代碼行數:22,代碼來源:jsonapi.go


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