• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang extract.New函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/opesun/extract.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了New函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: SwitchToTemplate

// Switches from one template to another.
// Fails if the template we want to switch does not exist.
func SwitchToTemplate(db *mgo.Database, inp map[string][]string, root, host string) error {
	rule := map[string]interface{}{
		"template_name": "must",
		"template_type": "must",
	}
	dat, e_err := extract.New(rule).Extract(inp)
	if e_err != nil {
		return e_err
	}
	template_type := dat["template_type"].(string)
	template_name := dat["template_name"].(string)
	var e bool
	var err error
	if template_type == "public" {
		e, err = Exists(filepath.Join(root, "templates/public", template_name))
	} else {
		e, err = Exists(filepath.Join(root, "templates/private", host, template_name))
	}
	if err != nil {
		return fmt.Errorf("Can't determine if template exists.")
	}
	if !e {
		return fmt.Errorf("%v template named %v does not exist.", template_type, template_name)
	}
	return switchToTemplateDb(db, template_type, template_name)
}
开发者ID:Laller,项目名称:hypecms,代码行数:28,代码来源:template_editor.go


示例2: insert

func insert(db *mgo.Database, ev ifaces.Event, rule map[string]interface{}, dat map[string][]string, user_id bson.ObjectId, fixvals map[string]interface{}) (bson.ObjectId, error) {
	// Could check for id here, alert if we found one.
	rule["type"] = "must"
	rule["draft_id"] = "must" // Can be draft, or version.
	ins_dat, extr_err := extract.New(rule).Extract(dat)
	if extr_err != nil {
		return "", extr_err
	}
	typ := ins_dat["type"].(string)
	basic.DateAndAuthor(rule, ins_dat, user_id, false)
	_, has_tags := ins_dat[Tag_fieldname_displayed]
	if has_tags {
		addTags(db, ins_dat, "", "insert", typ)
	}
	basic.Slug(rule, ins_dat)
	mergeMaps(ins_dat, fixvals)
	err := basic.InudVersion(db, ev, ins_dat, "contents", "insert", "")
	if err != nil {
		return "", err
	}
	ret_id := ins_dat["_id"].(bson.ObjectId)
	_, has_fulltext := rule["fulltext"]
	if has_fulltext {
		saveFulltext(db, ret_id)
	}
	return ret_id, nil
}
开发者ID:Laller,项目名称:hypecms,代码行数:27,代码来源:content_model.go


示例3: ChooseChild

// Example:
// {
//		"type":					"choose_child",
//		"c": 					"contents",
//		"choose_fieldname":		"accepted",			// This field will be set to true when the child document is chosen.
//		"parent_fieldname":		"parent",			// The name of the field in which the parent's id resides.
//		"max_choices":			1					// Optional, defaults to 1. Describes how many chosen children can exists per parent.
// }
//
// Gets chosen document to find out parent.
// Gets parent document, checks if user is the author of it. Return error if not.
// Sets choose_fieldname to the number of already chosen children + 1 in the chosen document.
// Increases "has_" + choose_fieldname by 1 in parent document.
func ChooseChild(db *mgo.Database, user, action map[string]interface{}, inp map[string][]string) error {
	choose_fieldname := action["choose_fieldname"].(string)
	parent_fieldname := action["parent_fieldname"].(string)
	coll := action["c"].(string)
	max_choices := 1
	mc, has_mc := action["max_choices"]
	if has_mc {
		max_choices = int(mc.(float64))
	}
	rule := m{
		"chosen_doc_id": "must",
	}
	dat, err := extract.New(rule).Extract(inp)
	if err != nil {
		return err
	}
	user_id := user["_id"].(bson.ObjectId)
	chosen_doc_id := patterns.ToIdWithCare(dat["chosen_doc_id"])
	parent_id, auth_err := IsAuthor(db, coll, parent_fieldname, user_id, chosen_doc_id)
	if auth_err != nil {
		return err
	}
	current_count, exc_err := Exceeded(db, coll, parent_fieldname, choose_fieldname, parent_id, max_choices)
	if exc_err != nil {
		return err
	}
	err = MarkAsChosen(db, coll, choose_fieldname, chosen_doc_id, current_count)
	if err != nil {
		return err
	}
	return IncrementParent(db, coll, choose_fieldname, parent_id, 1)
}
开发者ID:Laller,项目名称:hypecms,代码行数:45,代码来源:respond-choose_child.go


示例4: CanModifyComment

// Only called when doing update or delete.
// At inserting, user.OkayToDoAction is sufficient.
// This needs additional action: you can only update or delete a given comment only if it's yours (under a certain level).
func CanModifyComment(db *mgo.Database, inp map[string][]string, correction_level int, user_id bson.ObjectId, user_level int) error {
	rule := map[string]interface{}{
		"content_id": "must",
		"comment_id": "must",
		"type":       "must",
	}
	dat, err := extract.New(rule).Extract(inp)
	if err != nil {
		return err
	}
	// Even if he has the required level, and he is below correction_level, he can't modify other people's comment, only his owns.
	// So we query here the comment and check who is the owner of it.
	if user_level < correction_level {
		content_id_str := basic.StripId(dat["content_id"].(string))
		comment_id_str := basic.StripId(dat["comment_id"].(string))
		auth, err := findCommentAuthor(db, content_id_str, comment_id_str)
		if err != nil {
			return err
		}
		if auth.Hex() != user_id.Hex() {
			return fmt.Errorf("You are not the rightous owner of the comment.")
		}
	}
	return nil
}
开发者ID:Laller,项目名称:hypecms,代码行数:28,代码来源:comments.go


示例5: PublishPrivate

// Publish a private template, so others can use it too.
// Copies the whole directory of /templates/private/{host}/{current_template} to /templates/public/{input:public_name}
// Fails if a public template with the chosen name already exists.
func PublishPrivate(db *mgo.Database, opt map[string]interface{}, inp map[string][]string, root, host string) error {
	if scut.TemplateType(opt) == "public" {
		return fmt.Errorf("You can't publish your current template, because it is already public.")
	}
	rule := map[string]interface{}{
		"public_name": map[string]interface{}{
			"must": 1,
			"type": "string",
			"min":  2,
		},
	}
	dat, ex_err := extract.New(rule).Extract(inp)
	if ex_err != nil {
		return ex_err
	}
	public_name := dat["public_name"].(string)
	from := filepath.Join(root, "templates", "private", host, scut.TemplateName(opt))
	to := filepath.Join(root, "templates", "public", public_name)
	// copyrecur.CopyDir checks for existence too, but for safety reasons we check here in case copyrecur semantics change.
	exis, exis_err := Exists(to)
	if exis {
		return fmt.Errorf("Public template with name " + public_name + " already exists.")
	}
	if exis_err != nil {
		return exis_err
	}
	copy_err := copyrecur.CopyDir(from, to)
	if copy_err != nil {
		return fmt.Errorf("There was an error while copying.")
	}
	return nil
}
开发者ID:Laller,项目名称:hypecms,代码行数:35,代码来源:template_editor.go


示例6: UpdateComment

// Apart from rule, there are two mandatory field which must come from the UI: "content_id" and "comment_id"
func UpdateComment(db *mgo.Database, ev ifaces.Event, rule map[string]interface{}, inp map[string][]string, user_id bson.ObjectId) error {
	dat, err := extract.New(rule).Extract(inp)
	if err != nil {
		return err
	}
	basic.DateAndAuthor(rule, dat, user_id, true)
	ids, err := basic.ExtractIds(inp, []string{"content_id", "comment_id"})
	if err != nil {
		return err
	}
	comment_id := bson.ObjectIdHex(ids[1])
	q := bson.M{
		"_id": bson.ObjectIdHex(ids[0]),
		"comments.comment_id": comment_id,
	}
	upd := bson.M{
		"$set": bson.M{
			"comments.$": dat,
		},
	}
	err = db.C("contents").Update(q, upd)
	if err != nil {
		return err
	}
	return db.C("comments").Remove(m{"_id": comment_id})
}
开发者ID:Laller,项目名称:hypecms,代码行数:27,代码来源:comments.go


示例7: InstallB

// Install and Uninstall hooks all have the same signature: func (a *A)(bson.ObjectId) error
// InstallB handles both installing and uninstalling.
func InstallB(uni *context.Uni, mode string) error {
	if !requireLev(uni.Dat["_user"], 300) {
		return fmt.Errorf("No rights to install or uninstall a module.")
	}
	dat, err := extract.New(map[string]interface{}{"module": "must"}).Extract(uni.Req.Form)
	if err != nil {
		return err
	}
	modn := dat["module"].(string)
	uni.Dat["_cont"] = map[string]interface{}{"module": modn}
	obj_id, ierr := admin_model.InstallB(uni.Db, uni.Ev, uni.Opt, modn, mode)
	if ierr != nil {
		return ierr
	}
	if !uni.Caller.Has("hooks", modn, strings.Title(mode)) {
		return fmt.Errorf("Module %v does not export the Hook %v.", modn, mode)
	}
	//hook, ok := h.(func(*context.Uni, bson.ObjectId) error)
	//if !ok {
	//	return fmt.Errorf("%v hook of module %v has bad signature.", mode, modn)
	//}
	ret_rec := func(e error) {
		err = e
	}
	uni.Caller.Call("hooks", modn, strings.Title(mode), ret_rec, obj_id)
	return err
}
开发者ID:Laller,项目名称:hypecms,代码行数:29,代码来源:admin-m.go


示例8: update

func update(db *mgo.Database, ev ifaces.Event, rule map[string]interface{}, dat map[string][]string, user_id bson.ObjectId, fixvals map[string]interface{}) error {
	rule["id"] = "must"
	rule["type"] = "must"
	rule["draft_id"] = "must"
	upd_dat, extr_err := extract.New(rule).Extract(dat)
	if extr_err != nil {
		return extr_err
	}
	id := upd_dat["id"].(string)
	typ := upd_dat["type"].(string)
	basic.DateAndAuthor(rule, upd_dat, user_id, true)
	upd_dat["type"] = typ
	_, has_tags := upd_dat[Tag_fieldname_displayed]
	if has_tags {
		addTags(db, upd_dat, id, "update", typ)
	}
	basic.Slug(rule, upd_dat)
	mergeMaps(upd_dat, fixvals)
	err := basic.InudVersion(db, ev, upd_dat, Cname, "update", id)
	if err != nil {
		return err
	}
	_, has_fulltext := rule["fulltext"]
	id_bson := bson.ObjectIdHex(basic.StripId(id))
	if has_fulltext {
		saveFulltext(db, id_bson)
	}
	return nil
}
开发者ID:Laller,项目名称:hypecms,代码行数:29,代码来源:content_model.go


示例9: SolveTimer

func SolveTimer(secret string, inp map[string][]string, puzzle_opts map[string]interface{}) error {
	min_diff, ok := puzzle_opts["min_diff"].(int)
	if !ok {
		min_diff = 10
	}
	current := int(time.Now().Unix())
	r := map[string]interface{}{
		"__t": "must",
	}
	dat, err := extract.New(r).Extract(inp)
	if err != nil {
		return err
	}
	decrypted_v, err := decryptStr([]byte(secret_salt+secret), dat["__t"].(string))
	if err != nil {
		return err
	}
	stamp, err := strconv.Atoi(decrypted_v)
	if err != nil {
		return err
	}
	if current-stamp < min_diff {
		return fmt.Errorf("You submitted the form too quickly, wait %v seconds please.", min_diff)
	}
	return nil
}
开发者ID:Laller,项目名称:hypecms,代码行数:26,代码来源:puzzle.go


示例10: ChangeHead

// Implementation of versioning is in basic.InudVersion.
func ChangeHead(db *mgo.Database, ev ifaces.Event, inp map[string][]string, non_versioned_fields []string) error {
	rule := map[string]interface{}{
		"version_id": "must",
		"id":         "must",
	}
	dat, err := extract.New(rule).Extract(inp)
	if err != nil {
		return err
	}
	version_id_str := basic.StripId(dat["version_id"].(string))
	version_id := bson.ObjectIdHex(version_id_str)
	var v interface{}
	err = db.C(Cname + "_version").Find(bson.M{"_id": version_id}).One(&v)
	if err != nil {
		return err
	}
	revert_to := v.(bson.M)
	id := patterns.ToIdWithCare(dat["id"].(string))
	for _, v := range non_versioned_fields {
		delete(revert_to, v)
	}
	revert_to["points_to"] = revert_to["_id"]
	delete(revert_to, "id")
	return db.C(Cname).Update(bson.M{"_id": id}, bson.M{"$set": revert_to})
}
开发者ID:Laller,项目名称:hypecms,代码行数:26,代码来源:versions.go


示例11: igniteReadOps

func igniteReadOps(session *mgo.Session, db *mgo.Database, boots_opt map[string]interface{}, inp map[string][]string) (map[string]interface{}, string, error) {
	if session == nil {
		return nil, "", fmt.Errorf("This is not an admin instance.")
	}
	r := newSiteRules()
	dat, err := extract.New(r).Extract(inp)
	if err != nil {
		return nil, "", err
	}
	// Need to check it here too, not just at admin_model.RegFirstAdmin!
	if dat["password"].(string) != dat["password_again"].(string) {
		return nil, "", fmt.Errorf("Passwords do not match.")
	}
	max_cap := numcon.IntP(boots_opt["max_cap"])
	hasroom, err := hasRoom(db, max_cap)
	if err != nil {
		return nil, "", err
	}
	if !hasroom {
		return nil, "", fmt.Errorf("Server is at full capacity.")
	}
	sitename := dat["sitename"].(string)
	root_db := boots_opt["root_db"].(string)
	if sitename == root_db || strings.HasPrefix(sitename, "www") {
		return nil, "", fmt.Errorf("Sitename cant equal to root sitename or start with www.")
	}
	default_must := boots_opt["default_must"].(bool)
	def_opts, err := defaultOpts(db)
	if default_must && err != nil {
		return nil, "", fmt.Errorf("Cant read default option document.")
	}
	return def_opts, sitename, sitenameIsOk(db, sitename)
}
开发者ID:Laller,项目名称:hypecms,代码行数:33,代码来源:bootstrap_model.go


示例12: DeleteFile

// Delete a file OR dir. See NewFile for controversy about filenames and extensions.
func DeleteFile(opt map[string]interface{}, inp map[string][]string, root, host string) error {
	if !CanModifyTemplate(opt) {
		return fmt.Errorf(cant_mod_public)
	}
	rule := map[string]interface{}{
		"filepath": "must",
	}
	dat, e_err := extract.New(rule).Extract(inp)
	if e_err != nil {
		return e_err
	}
	fp := dat["filepath"].(string)
	full_p := filepath.Join(root, scut.GetTPath(opt, host), fp)
	var err error
	if IsDir(fp) {
		err = os.RemoveAll(full_p)
	} else {
		err = os.Remove(full_p)
	}
	if err != nil {
		fmt.Println(err)
		return fmt.Errorf("Can't delete file or directory. Probably it does not exist.")
	}
	return nil
}
开发者ID:Laller,项目名称:hypecms,代码行数:26,代码来源:template_editor.go


示例13: ForkPrivate

// Fork current private template into an other private one.
// Copies the whole directory from /templates/private/{host}/{current_template} to /templates/private/{host}/{inp:new_private_name}
func ForkPrivate(db *mgo.Database, opt map[string]interface{}, inp map[string][]string, root, host string) error {
	if scut.TemplateType(opt) != "private" {
		return fmt.Errorf("Your current template is not a private one.") // Kinda unsensical error message but ok...
	}
	rule := map[string]interface{}{
		"new_template_name": "must",
	}
	dat, e_err := extract.New(rule).Extract(inp)
	if e_err != nil {
		return e_err
	}
	new_template_name := dat["new_template_name"].(string)
	to := filepath.Join(root, "templates", "private", host, new_template_name)
	e, e_err := Exists(to)
	if e_err != nil {
		return fmt.Errorf("Can't determine if private template exists.")
	}
	if e {
		return fmt.Errorf("Private template named %v already exists.", new_template_name)
	}
	from := filepath.Join(root, "templates", "private", host, scut.TemplateName(opt))
	copy_err := copyrecur.CopyDir(from, to)
	if copy_err != nil {
		return fmt.Errorf("There was an error while copying.")
	}
	id := basic.CreateOptCopy(db)
	q := m{"_id": id}
	upd := m{
		"$set": m{
			"Template": new_template_name,
		},
	}
	return db.C("options").Update(q, upd)
}
开发者ID:Laller,项目名称:hypecms,代码行数:36,代码来源:template_editor.go


示例14: SaveDraft

// We never update drafts, we always insert a new one.
// A draft will have the next fields: id, type, created, up_to_date, parent_content/draft_content/none/both, data.
// The saved input resides in the data.
func SaveDraft(db *mgo.Database, content_rules map[string]interface{}, inp map[string][]string) (bson.ObjectId, error) {
	for i, _ := range content_rules {
		content_rules[i] = 1
	}
	content_rules["type"] = "must"
	content_rules["id"] = "must"       // Content id.
	content_rules["draft_id"] = "must" // Draft id, no empty if we save draft from a draft, empty if draft is saved from a content.
	dat, err := extract.New(content_rules).Extract(inp)
	if err != nil {
		return "", err
	}
	live_id_s := dat["id"].(string)
	draft_id_s := dat["draft_id"].(string)
	ins := m{
		"created": time.Now().Unix(),
		"data":    dat,
		"type":    dat["type"],
	}
	var parent, root bson.ObjectId
	if len(draft_id_s) > 0 { // Coming from a draft.
		draft_id := patterns.ToIdWithCare(draft_id_s)
		var last_version bson.ObjectId
		parent, root, last_version, err = basic.GetDraftParent(db, "contents", draft_id)
		if parent == "" || root == "" {
			return "", fmt.Errorf("Coming from draft but still can not extract parent or root.")
		}
		// Has no version of it is a draft/descendant of a draft which connects to no saved content.
		if last_version != "" {
			ins["draft_of_version"] = last_version
		}
	} else if len(live_id_s) > 0 { // Coming from a content.
		live_id := patterns.ToIdWithCare(live_id_s)
		// This way draft saves coming from any versions will be saved to the version pointed to by the head anyway...
		parent, root, err = basic.GetParentTroughContent(db, "contents", live_id)
		// If we have a live content saved, we must have a version to point to too.
		if parent == "" || root == "" {
			return "", fmt.Errorf("Coming from content but still can not extract parent or root.")
		}
		ins["draft_of_version"] = parent
	}
	if err != nil {
		return "", err
	}
	if len(live_id_s) > 0 {
		live_id := patterns.ToIdWithCare(live_id_s)
		ins["draft_of"] = live_id // Just so we can display the content id for saving content immediately from a draft.
	}
	draft_id := bson.NewObjectId()
	if parent != "" { // If there is a parent there is a root...
		ins["-parent"] = parent
		ins["root"] = root
	} else {
		ins["root"] = draft_id
	}
	// Note: we dont store the last version here, building the draft will be harder.
	ins["_id"] = draft_id
	err = db.C(Cname + Draft_collection_postfix).Insert(ins)
	return draft_id, err
}
开发者ID:Laller,项目名称:hypecms,代码行数:62,代码来源:versions.go


示例15: DeleteSite

// Currently we wonly delete a site from the sites collection of the root database, for safety reasons.
// The deletion will only take effect at next restart.
func DeleteSite(db *mgo.Database, inp map[string][]string) error {
	r := map[string]interface{}{
		"sitename": "must",
	}
	dat, err := extract.New(r).Extract(inp)
	if err != nil {
		return err
	}
	return deleteSite(db, dat["sitename"].(string))
}
开发者ID:Laller,项目名称:hypecms,代码行数:12,代码来源:bootstrap_model.go


示例16: MoveToFinalWE

// MoveToFinal with extract.
func MoveToFinalWE(db *mgo.Database, inp map[string][]string) error {
	r := map[string]interface{}{
		"comment_id": "must",
	}
	dat, err := extract.New(r).Extract(inp)
	if err != nil {
		return err
	}
	comment_id := basic.ToIdWithCare(dat["comment_id"])
	return MoveToFinal(db, comment_id)
}
开发者ID:Laller,项目名称:hypecms,代码行数:12,代码来源:comments.go


示例17: FromWeb

func FromWeb(uni *context.Uni) error {
	dat, err := extract.New(map[string]interface{}{"commands": "must"}).Extract(uni.Req.Form)
	if err != nil {
		return err
	}
	res, err := Run(uni, dat["commands"].(string))
	if err != nil {
		return err
	}
	uni.Dat["_cont"] = map[string]interface{}{"output": res}
	return nil
}
开发者ID:Laller,项目名称:hypecms,代码行数:12,代码来源:shell.go


示例18: SaveTypeConfig

func SaveTypeConfig(db *mgo.Database, inp map[string][]string) error {
	rule := map[string]interface{}{
		"type":        "must",
		"safe_delete": "must",
	}
	_, err := extract.New(rule).Extract(inp) // _ = dat
	if err != nil {
		return err
	}
	// TODO: finish.
	return nil
}
开发者ID:Laller,项目名称:hypecms,代码行数:12,代码来源:content_model.go


示例19: SaveFile

// Save an existing file.
func SaveFile(opt map[string]interface{}, inp map[string][]string, root, host string) error {
	if !CanModifyTemplate(opt) {
		return fmt.Errorf(cant_mod_public)
	}
	rule := map[string]interface{}{
		"filepath": "must",
		"content":  "must",
	}
	dat, e_err := extract.New(rule).Extract(inp)
	if e_err != nil {
		return e_err
	}
	fp := dat["filepath"].(string)
	content := dat["content"].(string)
	return ioutil.WriteFile(filepath.Join(root, scut.GetTPath(opt, host), fp), []byte(content), os.ModePerm)
}
开发者ID:Laller,项目名称:hypecms,代码行数:17,代码来源:template_editor.go


示例20: FindLogin

// Everyone uses this to log in, admins, users, guest users and their mom.
func FindLogin(db *mgo.Database, inp map[string][]string) (map[string]interface{}, bson.ObjectId, error) {
	rule := map[string]interface{}{
		"name":     "must",
		"password": "must",
	}
	d, err := extract.New(rule).Extract(inp)
	if err != nil {
		return nil, "", err
	}
	name := d["name"].(string)
	pass := EncodePass(d["password"].(string))
	user, err := namePass(db, name, pass)
	if err != nil {
		return nil, "", err
	}
	return user, user["_id"].(bson.ObjectId), nil
}
开发者ID:Laller,项目名称:hypecms,代码行数:18,代码来源:user_model.go



注:本文中的github.com/opesun/extract.New函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang goquery.ParseUrl函数代码示例发布时间:2022-05-28
下一篇:
Golang interfaces.Filter类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap