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

Golang schema.NewExtension函数代码示例

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

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



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

示例1: BenchmarkDonburi

func BenchmarkDonburi(b *testing.B) {
	beforeEach()
	defer afterEach()
	donburi, _ := schema.NewExtension(map[string]interface{}{
		"id":   "donburi",
		"url":  "file://../../etc/extensions/donburi.js",
		"path": ".*",
	})
	extension, _ := schema.NewExtension(map[string]interface{}{
		"id": "test_donburi",
		"code": `tasks:
  - vars:
      message: "hello"
  - vars:
      template_test: "message: {{.message}}"
`,
		"path":      ".*",
		"code_type": "donburi",
	})

	context := map[string]interface{}{}

	extensions := []*schema.Extension{donburi, extension}
	env := NewEnvironment(dataStore, &middleware.FakeIdentity{})
	err := env.LoadExtensionsForPath(extensions, "test_path")
	if err != nil {
		b.Error(err)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		env.HandleEvent("post_create", context)
	}
}
开发者ID:gitter-badger,项目名称:gohan,代码行数:34,代码来源:donburi_test.go


示例2: TestDonburiResources

func TestDonburiResources(t *testing.T) {
	RegisterTestingT(t)
	beforeEach()
	defer afterEach()
	donburi, err := schema.NewExtension(map[string]interface{}{
		"id":   "donburi",
		"url":  "file://../../etc/extensions/donburi.js",
		"path": ".*",
	})
	if err != nil {
		t.Error(err)
	}

	extension, err := schema.NewExtension(map[string]interface{}{
		"id": "test_donburi",
		"code": `tasks:
    - vars:
        failed: false
    - resources:
      - vars:
          dependent: true
      - eval: "if(dependent){failed = true}"
        when: event_type == "pre_delete"
      - eval: "if(!dependent){failed = true}"
        when: event_type == "post_create"
      - vars:
          dependent: false
`,
		"path":      ".*",
		"code_type": "donburi",
	})
	if err != nil {
		t.Error(err)
	}

	context := map[string]interface{}{}

	extensions := []*schema.Extension{donburi, extension}
	env := NewEnvironment(dataStore, &middleware.FakeIdentity{})
	err = env.LoadExtensionsForPath(extensions, "test_path")
	if err != nil {
		t.Error(err)
	}
	err = env.HandleEvent("post_create", context)
	if err != nil {
		t.Error(err)
	}
	Expect(context["failed"]).To(Equal(false))
	err = env.HandleEvent("pre_delete", context)
	if err != nil {
		t.Error(err)
	}
	Expect(context["failed"]).To(Equal(false))
}
开发者ID:gitter-badger,项目名称:gohan,代码行数:54,代码来源:donburi_test.go


示例3: TestDonburiInjectionAttack

func TestDonburiInjectionAttack(t *testing.T) {
	RegisterTestingT(t)
	beforeEach()
	defer afterEach()
	donburi, err := schema.NewExtension(map[string]interface{}{
		"id":   "donburi",
		"url":  "file://../../etc/extensions/donburi.js",
		"path": ".*",
	})
	if err != nil {
		t.Error(err)
	}

	extension, err := schema.NewExtension(map[string]interface{}{
		"id": "test_donburi",
		"code": `tasks:
    - vars:
        executed: true
      when: code
    - eval: "{{.code}}"
    - block:
        - eval: "throw error"
          rescue:
            - eval "{{.code}}"
          always:
            - eval "{{.code}}"
          when: code
`,
		"path":      ".*",
		"code_type": "donburi",
	})
	if err != nil {
		t.Error(err)
	}

	context := map[string]interface{}{
		"attacked": false,
		"code":     "context['attacked'] = true",
	}

	extensions := []*schema.Extension{donburi, extension}
	env := NewEnvironment(dataStore, &middleware.FakeIdentity{})
	err = env.LoadExtensionsForPath(extensions, "test_path")
	if err != nil {
		t.Error(err)
	}
	err = env.HandleEvent("post_create", context)
	if err != nil {
		t.Error(err)
	}
	Expect(context["attacked"]).To(Equal(false))
}
开发者ID:gitter-badger,项目名称:gohan,代码行数:52,代码来源:donburi_test.go


示例4: TestDonburiExec

func TestDonburiExec(t *testing.T) {
	RegisterTestingT(t)
	beforeEach()
	defer afterEach()
	donburi, err := schema.NewExtension(map[string]interface{}{
		"id":   "donburi",
		"url":  "file://../../etc/extensions/donburi.js",
		"path": ".*",
	})
	if err != nil {
		t.Error(err)
	}

	extension, err := schema.NewExtension(map[string]interface{}{
		"id": "test_donburi",
		"code": `tasks:
    - command:
       name: echo
       args:
         - test
      register: result
    - command:
       name: no_command
       args: []
      register: result2
`,
		"path":      ".*",
		"code_type": "donburi",
	})
	if err != nil {
		t.Error(err)
	}

	context := map[string]interface{}{}

	extensions := []*schema.Extension{donburi, extension}
	env := NewEnvironment(dataStore, &middleware.FakeIdentity{})
	err = env.LoadExtensionsForPath(extensions, "test_path")
	if err != nil {
		t.Error(err)
	}
	err = env.HandleEvent("post_create", context)
	if err != nil {
		t.Error(err)
	}
	output := context["result"].(map[string]string)
	Expect(output["status"]).To(Equal("success"))
	Expect(output["output"]).To(Equal("test\n"))
	output2 := context["result2"].(map[string]string)
	Expect(output2["status"]).To(Equal("error"))
}
开发者ID:gitter-badger,项目名称:gohan,代码行数:51,代码来源:donburi_test.go


示例5: TestDefine

func TestDefine(t *testing.T) {
	RegisterTestingT(t)
	beforeEach()
	defer afterEach()
	donburi, err := schema.NewExtension(map[string]interface{}{
		"id":   "donburi",
		"url":  "file://../../etc/extensions/donburi.js",
		"path": ".*",
	})
	if err != nil {
		t.Error(err)
	}

	extension, err := schema.NewExtension(map[string]interface{}{
		"id": "test_donburi",
		"code": `tasks:
  - define:
     name: "add"
     tasks:
     - eval: "left+right"
  - add:
      left: 1
      right: 2
    register: answer
`,
		"path":      ".*",
		"code_type": "donburi",
	})
	if err != nil {
		t.Error(err)
	}

	context := map[string]interface{}{}

	extensions := []*schema.Extension{donburi, extension}
	env := NewEnvironment(dataStore, &middleware.FakeIdentity{}, timelimit)
	err = env.LoadExtensionsForPath(extensions, "test_path")
	if err != nil {
		t.Error(err)
	}
	err = env.HandleEvent("post_create", context)
	if err != nil {
		t.Error(err)
	}
	Expect(context["answer"]).To(Equal(float64(3)))
}
开发者ID:masaki-saeki,项目名称:gohan,代码行数:46,代码来源:donburi_test.go


示例6:

			err = clearTable(tx, schema)
			Expect(err).ToNot(HaveOccurred(), "Failed to clear table.")
		}
		err = tx.Commit()
		Expect(err).ToNot(HaveOccurred(), "Failed to commite transaction.")

		extension.ClearManager()
	})

	Describe("Loading an extension", func() {
		Context("When extension uses code property", func() {
			It("should run the extension code", func() {
				extension, err := schema.NewExtension(map[string]interface{}{
					"id": "test_extension",
					"code": `tasks:
                             - vars:
                                 person: John
                     `,
					"path": ".*",
				})
				Expect(err).ToNot(HaveOccurred())
				extensions := []*schema.Extension{extension}
				env := gohanscript.NewEnvironment(timelimit)
				Expect(env.LoadExtensionsForPath(extensions, "test_path")).To(Succeed())

				context := map[string]interface{}{
					"id": "test",
				}
				Expect(env.HandleEvent("test_event", context)).To(Succeed())
				Expect(context["person"]).ToNot(BeNil())
			})
		})
开发者ID:cloudwan,项目名称:gohan,代码行数:32,代码来源:env_test.go


示例7:

		Expect(err).ToNot(HaveOccurred())
		r1, err = schema.NewResource(s, fakeResources[1])
		Expect(err).ToNot(HaveOccurred())

	})

	Describe("gohan_db_list", func() {
		Context("When valid minimum parameters are given", func() {
			It("returns the list ordered by id", func() {
				extension, err := schema.NewExtension(map[string]interface{}{
					"id": "test_extension",
					"code": `
					  gohan_register_handler("test_event", function(context){
					    var tx = context.transaction;
					    context.resp = gohan_db_list(
					      tx,
					      "test",
					      {"tenant_id": "tenant0"}
					    );
					  });`,
					"path": ".*",
				})
				Expect(err).ToNot(HaveOccurred())
				env := newEnvironmentWithExtension(extension)

				var pagenator *pagination.Paginator
				var fakeTx = new(mocks.Transaction)
				fakeTx.On(
					"List", s, transaction.Filter{"tenant_id": "tenant0"}, pagenator,
				).Return(
					[]*schema.Resource{r0, r1},
开发者ID:vozhyk-,项目名称:gohan,代码行数:31,代码来源:gohan_db_test.go


示例8:

			}
			err = clearTable(tx, schema)
			Expect(err).ToNot(HaveOccurred(), "Failed to clear table.")
		}
		err = tx.Commit()
		Expect(err).ToNot(HaveOccurred(), "Failed to commite transaction.")

		extension.ClearManager()
	})

	Describe("Loading an extension", func() {
		Context("When extension is not a valid JavaScript", func() {
			It("returns a meaningful compilation error", func() {
				goodExtension, err := schema.NewExtension(map[string]interface{}{
					"id":   "good_extension",
					"code": `gohan_register_handler("test_event", function(context) {});`,
					"path": ".*",
				})
				Expect(err).ToNot(HaveOccurred())
				goodExtension.URL = "good_extension.js"

				badExtension, err := schema.NewExtension(map[string]interface{}{
					"id":   "bad_extension",
					"code": `gohan_register_handler("test_event", function(context {});`,
					"path": ".*",
				})
				Expect(err).ToNot(HaveOccurred())
				badExtension.URL = "bad_extension.js"

				extensions := []*schema.Extension{goodExtension, badExtension}
				env := otto.NewEnvironment(testDB, &middleware.FakeIdentity{})
开发者ID:nati,项目名称:gohan,代码行数:31,代码来源:otto_test.go


示例9: TestDonburiFlows

func TestDonburiFlows(t *testing.T) {
	RegisterTestingT(t)
	beforeEach()
	defer afterEach()
	donburi, err := schema.NewExtension(map[string]interface{}{
		"id":   "donburi",
		"url":  "file://../../etc/extensions/donburi.js",
		"path": ".*",
	})
	if err != nil {
		t.Error(err)
	}

	extension, err := schema.NewExtension(map[string]interface{}{
		"id": "test_donburi",
		"code": `tasks:
  - eval: "1 + 1"
    register: result
  - eval: "true"
    register: when_is_working
    when: "result == 2"
  - block:
    - vars:
        list2 : [4, 5, 6]
    - eval: "result += item"
      with_items:
       - 1
       - 2
       - 3
    when: when_is_working
  - vars:
      message: "hello"
  - vars:
      retry_count: 0
      rescue_executed: false
      always_executed: false
      template_test: "message: {{.message}}"
  - eval: "result += item"
    with_items: "list2"
  - eval: "context[item.key] = item.value"
    with_dict:
      alice: 18
      bob: 21
  - block:
    - sleep: 10
    - eval: retry_count += 1
    - eval: throw 'error'
    rescue:
    - eval: "rescue_executed = true"
    always:
    - eval: "always_executed = true"
    retry: 3
`,
		"path":      ".*",
		"code_type": "donburi",
	})
	if err != nil {
		t.Error(err)
	}

	context := map[string]interface{}{}

	extensions := []*schema.Extension{donburi, extension}
	env := NewEnvironment(dataStore, &middleware.FakeIdentity{})
	err = env.LoadExtensionsForPath(extensions, "test_path")
	if err != nil {
		t.Error(err)
	}
	err = env.HandleEvent("post_create", context)
	if err != nil {
		t.Error(err)
	}
	Expect(context["result"]).To(Equal(float64(23)))
	Expect(context["when_is_working"]).To(Equal(true))
	Expect(context["rescue_executed"]).To(Equal(true))
	Expect(context["always_executed"]).To(Equal(true))
	Expect(context["template_test"]).To(Equal("message: hello"))
	Expect(context["alice"]).To(Equal(int(18)))
	Expect(context["retry_count"]).To(Equal(float64(3)))
}
开发者ID:gitter-badger,项目名称:gohan,代码行数:80,代码来源:donburi_test.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang schema.Manager类代码示例发布时间:2022-05-23
下一篇:
Golang schema.NewAuthorization函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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