Ejemplo n.º 1
0
func TestNode_Label(t *testing.T) {
	n := node.NewNode()
	n.Parent = node.NewNode()
	n.Key = ""
	n.Value = ""
	n.Index = -1

	assert.Equal(t, "(?)", n.Label(context.Background()))

	n.Index = 1
	assert.Equal(t, "1", n.Label(context.Background()))

	n.Value = &lab{}
	assert.Equal(t, "b", n.Label(context.Background()))

	n.Key = "a"
	assert.Equal(t, "a", n.Label(context.Background()))

	n.Value = &system.Object{Id: system.NewReference("a.b/c", "d")}
	n.Parent = nil
	assert.Equal(t, "d", n.Label(context.Background()))

	n.Value = nil
	assert.Equal(t, "root", n.Label(context.Background()))

	n = nil
	assert.Equal(t, "(nil)", n.Label(context.Background()))

}
Ejemplo n.º 2
0
func TestNode_InitialiseMapChild(t *testing.T) {
	cb, n := data.Setup(t)
	test := func(t *testing.T, n *node.Node, m *data.Multi) {

		c := node.NewNode()
		require.NoError(t, c.InitialiseMapItem(cb.Ctx(), n.Map["mjs"], "c"))
		require.NoError(t, c.AddToMap(cb.Ctx(), n.Map["mjs"], "c", true))
		assert.Equal(t, 3, len(n.Map["mjs"].Map))
		assert.True(t, n.Map["mjs"].Map["c"].Null)
		assert.False(t, n.Map["mjs"].Map["c"].Missing)
		assert.Equal(t, c, n.Map["mjs"].Map["c"])
		assert.Equal(t, "", m.Mjs["c"])

		c1 := node.NewNode()
		require.NoError(t, c1.InitialiseMapItem(cb.Ctx(), n.Map["mss"], "c"))
		require.NoError(t, c1.AddToMap(cb.Ctx(), n.Map["mss"], "c", true))
		assert.Equal(t, 3, len(n.Map["mss"].Map))
		assert.Nil(t, m.Mss["c"])

		c2 := node.NewNode()
		require.NoError(t, c2.InitialiseMapItem(cb.Ctx(), n.Map["mm"], "c"))
		require.NoError(t, c2.AddToMap(cb.Ctx(), n.Map["mm"], "c", true))
		assert.Equal(t, 3, len(m.Mm))
		assert.Nil(t, m.Mm["c"])
	}
	data.Run(t, n, n.Value.(*data.Multi), test)

}
Ejemplo n.º 3
0
func TestNode_Path(t *testing.T) {
	r := node.NewNode()
	a := node.NewNode()
	b := node.NewNode()
	a.Key = "a"
	b.Key = "b"
	a.Parent = r
	b.Parent = a
	assert.Equal(t, "root/a/b", b.Path())
}
Ejemplo n.º 4
0
func TestNode_Root(t *testing.T) {
	r := node.NewNode()
	a := node.NewNode()
	b := node.NewNode()
	a.Parent = r
	b.Parent = a
	assert.Equal(t, r, a.Root())
	assert.Equal(t, r, b.Root())
	n := (*node.Node)(nil)
	assert.Nil(t, n.Root())
}
Ejemplo n.º 5
0
func TestDeleteMutation1(t *testing.T) {
	cb, n := data.Setup(t)
	test := func(t *testing.T, n *node.Node, m *data.Multi) {
		var b, d, p *node.Node
		b = node.NewNode()
		d = n.Map["am"].Array[0]
		p = n.Map["am"]
		assert.Equal(t, "amjs0", n.Map["am"].Array[0].Map["js"].ValueString)
		assert.Equal(t, "amjs1", n.Map["am"].Array[1].Map["js"].ValueString)
		assert.Equal(t, "amjs0", m.Am[0].Js)
		assert.Equal(t, "amjs1", m.Am[1].Js)
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.Equal(t, 1, len(n.Map["am"].Array))
		assert.Equal(t, 1, len(m.Am))
		assert.Equal(t, "amjs1", n.Map["am"].Array[0].Map["js"].ValueString)
		assert.Equal(t, "amjs1", m.Am[0].Js)
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.Equal(t, 2, len(n.Map["am"].Array))
		assert.Equal(t, 2, len(m.Am))
		assert.Equal(t, "amjs0", n.Map["am"].Array[0].Map["js"].ValueString)
		assert.Equal(t, "amjs1", n.Map["am"].Array[1].Map["js"].ValueString)
		assert.Equal(t, "amjs0", m.Am[0].Js)
		assert.Equal(t, "amjs1", m.Am[1].Js)
	}
	test(t, n.Map["m"], n.Value.(*data.Multi).M)
}
Ejemplo n.º 6
0
func TestNode_Unpack5(t *testing.T) {

	cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)

	s := `{
	"type": "multi",
	"sri": {"type":"system:string", "value": "b"},
	"nri": {"type":"system:number", "value": 2},
	"bri": {"type":"system:bool", "value": true},
	"aljbi": {"type":"aljb", "value": true},
	"aljni": {"type":"aljn", "value": 3.0},
	"aljsi": {"type":"aljs", "value": "aljs2"}
}`

	n := node.NewNode()
	err := n.Unpack(cb.Ctx(), system.MustPackString(s), false)
	require.NoError(t, err)
	assert.Equal(t, "b", n.Value.(*data.Multi).Sri.GetString(cb.Ctx()).Value())
	assert.Equal(t, 2.0, n.Value.(*data.Multi).Nri.GetNumber(cb.Ctx()).Value())
	assert.Equal(t, true, n.Value.(*data.Multi).Bri.GetBool(cb.Ctx()).Value())

	assert.Equal(t, data.Aljb(true), *n.Value.(*data.Multi).Aljbi.GetAljb(cb.Ctx()))
	assert.Equal(t, data.Aljn(3.0), *n.Value.(*data.Multi).Aljni.GetAljn(cb.Ctx()))
	assert.Equal(t, data.Aljs("aljs2"), *n.Value.(*data.Multi).Aljsi.GetAljs(cb.Ctx()))

}
Ejemplo n.º 7
0
func TestAddMutationRoot(t *testing.T) {
	cb, _ := data.Setup(t)
	a := node.NewNode()
	ty, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/tests/data", "multi")
	require.True(t, ok)
	require.NoError(t, mutateAddNode(cb.Ctx(), a, nil, "", 2, ty, "z"))
	require.NotNil(t, a.Value.(*data.Multi).Id)
	require.Equal(t, "z", a.Value.(*data.Multi).Id.Name)
}
Ejemplo n.º 8
0
func TestNode_InitialiseArrayChild(t *testing.T) {
	cb, n := data.Setup(t)
	test := func(t *testing.T, n *node.Node, m *data.Multi) {
		c := node.NewNode()
		require.NoError(t, c.InitialiseArrayItem(cb.Ctx(), n.Map["ajs"], 4))
		require.NoError(t, c.AddToArray(cb.Ctx(), n.Map["ajs"], 4, true))
		assert.Equal(t, 5, len(n.Map["ajs"].Array))
		assert.True(t, n.Map["ajs"].Array[4].Null)
		assert.False(t, n.Map["ajs"].Array[4].Missing)
		assert.Equal(t, c, n.Map["ajs"].Array[4])
		assert.Equal(t, []string{"ajs0", "ajs1", "ajs2", "ajs3", ""}, m.Ajs)

		c1 := node.NewNode()
		require.NoError(t, c1.InitialiseArrayItem(cb.Ctx(), n.Map["ass"], 4))
		require.NoError(t, c1.AddToArray(cb.Ctx(), n.Map["ass"], 4, true))
		assert.Equal(t, 5, len(m.Ass))
		assert.Nil(t, m.Ass[4])

		c2 := node.NewNode()
		require.NoError(t, c2.InitialiseArrayItem(cb.Ctx(), n.Map["ajs"], 0))
		require.NoError(t, c2.AddToArray(cb.Ctx(), n.Map["ajs"], 0, true))
		assert.Equal(t, 6, len(n.Map["ajs"].Array))
		assert.Equal(t, []string{"", "ajs0", "ajs1", "ajs2", "ajs3", ""}, m.Ajs)

		c3 := node.NewNode()
		require.NoError(t, c3.InitialiseArrayItem(cb.Ctx(), n.Map["ass"], 0))
		require.NoError(t, c3.AddToArray(cb.Ctx(), n.Map["ass"], 0, true))
		assert.Equal(t, 6, len(m.Ass))
		assert.Nil(t, m.Ass[0])

		c4 := node.NewNode()
		require.NoError(t, c4.InitialiseArrayItem(cb.Ctx(), n.Map["am"], 2))
		require.NoError(t, c4.AddToArray(cb.Ctx(), n.Map["am"], 2, true))
		assert.Equal(t, 3, len(m.Am))
		assert.Nil(t, m.Am[2])
	}
	data.Run(t, n, n.Value.(*data.Multi), test)

}
Ejemplo n.º 9
0
Archivo: helpers.go Proyecto: kego/ke
func Empty(t *testing.T) (*tests.ContextBuilder, *node.Node) {

	cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)

	s := `{
	"type": "multi",
	"m": { "type": "multi" },
	"am": [ { "type": "multi" }, { "type": "multi" } ],
	"mm": { "a": { "type": "multi" }, "b": { "type": "multi" } }
}`
	n := node.NewNode()
	require.NoError(t, n.Unpack(cb.Ctx(), system.MustPackString(s), false))
	return cb, n
}
Ejemplo n.º 10
0
func TestNode_InitialiseArrayChild2(t *testing.T) {
	cb, n := data.Setup(t)
	c := node.NewNode()
	require.NoError(t, c.InitialiseArrayItem(cb.Ctx(), n.Map["ajs"], 5))
	err := c.AddToArray(cb.Ctx(), n.Map["ajs"], 5, true)
	assert.IsError(t, err, "GHJIDXABLL")

	// kludge the Val to be empty
	n.Map["ajs"].Val.Set(reflect.ValueOf([]string{}))
	require.NoError(t, c.InitialiseArrayItem(cb.Ctx(), n.Map["ajs"], 1))
	err = c.AddToArray(cb.Ctx(), n.Map["ajs"], 1, true)
	assert.IsError(t, err, "YFKMXFUPHY")

}
Ejemplo n.º 11
0
func TestNode_Unpack3(t *testing.T) {

	cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)

	s := `{
	"type": "multi",
	"a": "b"
}`

	n := node.NewNode()
	err := n.Unpack(cb.Ctx(), system.MustPackString(s), false)
	assert.HasError(t, err, "SRANLETJRS")

}
Ejemplo n.º 12
0
func TestAddMutation2(t *testing.T) {
	cb, n := data.Setup(t)
	test := func(t *testing.T, n *node.Node, m *data.Multi) {
		var a, p, b *node.Node
		a = node.NewNode()
		p = n.Map["am"]
		b = node.NewNode()
		require.NoError(t, mutateAddNode(cb.Ctx(), a, p, "", 0, nil, ""))
		assert.Equal(t, 3, len(n.Map["am"].Array))
		assert.Equal(t, 3, len(n.Map["am"].Value.([]*data.Multi)))
		assert.Equal(t, 3, len(m.Am))
		assert.Equal(t, "", m.Am[0].Js)
		assert.Equal(t, "amjs0", m.Am[1].Js)
		assert.Equal(t, "amjs1", m.Am[2].Js)
		require.NoError(t, mutateDeleteNode(cb.Ctx(), a, p, b))
		assert.Equal(t, 2, len(n.Map["am"].Array))
		assert.Equal(t, 2, len(n.Map["am"].Value.([]*data.Multi)))
		assert.Equal(t, 2, len(m.Am))
		assert.Equal(t, "amjs0", m.Am[0].Js)
		assert.Equal(t, "amjs1", m.Am[1].Js)
	}
	test(t, n.Map["m"], n.Value.(*data.Multi).M)
}
Ejemplo n.º 13
0
func TestAddMutationRedo(t *testing.T) {
	cb, n := data.Setup(t)

	test := func(t *testing.T, n *node.Node, m *data.Multi) {

		var a, p, b, a1, p1, b1 *node.Node
		a = node.NewNode()
		p = n.Map["am"]
		b = node.NewNode()
		ty, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/tests/data", "multi")
		require.True(t, ok)
		require.NoError(t, mutateAddNode(cb.Ctx(), a, p, "", 2, ty, ""))
		require.Equal(t, `[{"js":"amjs0","type":"multi"},{"js":"amjs1","type":"multi"},{"type":"multi"}]`, p.Print(cb.Ctx()))

		a1 = n.Map["am"].Array[2].Map["m"]
		p1 = n.Map["am"].Array[2]
		b1 = node.NewNode()
		require.NoError(t, mutateAddNode(cb.Ctx(), a1, p1, "m", -1, ty, ""))
		require.Equal(t, 3, len(n.Map["am"].Array))
		require.Equal(t, 3, len(m.Am))
		require.False(t, n.Map["am"].Array[2].Map["m"].Missing)
		require.Equal(t, `[{"js":"amjs0","type":"multi"},{"js":"amjs1","type":"multi"},{"m":{"type":"multi"},"type":"multi"}]`, p.Print(cb.Ctx()))

		require.NoError(t, mutateDeleteNode(cb.Ctx(), a, p, b))
		require.NoError(t, mutateDeleteNode(cb.Ctx(), a1, p1, b1))
		require.Equal(t, 2, len(n.Map["am"].Array))
		require.Equal(t, 2, len(m.Am))
		require.NoError(t, mutateRestoreNode(cb.Ctx(), a, p, b))
		require.NoError(t, mutateRestoreNode(cb.Ctx(), a1, p1, b1))
		require.Equal(t, 3, len(n.Map["am"].Array))
		require.Equal(t, 3, len(m.Am))
		require.NotNil(t, n.Map["am"].Array[2])
		require.NotNil(t, m.Am[2])
	}

	test(t, n.Map["m"], n.Value.(*data.Multi).M)
}
Ejemplo n.º 14
0
func TestNode_Unpack(t *testing.T) {

	cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)

	s := `{
	"type": "multi",
	"js": "a"
}`

	n := node.NewNode()
	require.NoError(t, n.Unpack(cb.Ctx(), system.MustPackString(s), false))
	m, ok := n.Value.(*data.Multi)
	require.True(t, ok)
	assert.Equal(t, "a", m.Js)

}
Ejemplo n.º 15
0
func TestAddMutation(t *testing.T) {

	cb, n := data.Setup(t)

	test := func(t *testing.T, n *node.Node, m *data.Multi) {
		var a, p, b *node.Node

		a = node.NewNode()
		p = n.Map["ajs"]
		b = node.NewNode()
		require.NoError(t, mutateAddNode(cb.Ctx(), a, p, "", 0, nil, ""))
		assert.Equal(t, 5, len(n.Map["ajs"].Array))
		assert.Equal(t, 5, len(m.Ajs))

		require.NoError(t, mutateDeleteNode(cb.Ctx(), a, p, b))
		assert.Equal(t, 4, len(n.Map["ajs"].Array))
		assert.Equal(t, 4, len(m.Ajs))

		a = node.NewNode()
		p = n.Map["ass"]
		b = node.NewNode()
		require.NoError(t, mutateAddNode(cb.Ctx(), a, p, "", 0, nil, ""))
		assert.Equal(t, 5, len(n.Map["ass"].Array))
		assert.Equal(t, 5, len(m.Ass))
		require.NoError(t, mutateDeleteNode(cb.Ctx(), a, p, b))
		assert.Equal(t, 4, len(n.Map["ass"].Array))
		assert.Equal(t, 4, len(m.Ass))

		a = node.NewNode()
		p = n.Map["am"]
		b = node.NewNode()
		require.NoError(t, mutateAddNode(cb.Ctx(), a, p, "", 0, nil, ""))
		assert.Equal(t, 3, len(n.Map["am"].Array))
		assert.Equal(t, 3, len(n.Map["am"].Value.([]*data.Multi)))
		assert.Equal(t, 3, len(m.Am))
		require.NoError(t, mutateDeleteNode(cb.Ctx(), a, p, b))
		assert.Equal(t, 2, len(n.Map["am"].Array))
		assert.Equal(t, 2, len(n.Map["am"].Value.([]*data.Multi)))
		assert.Equal(t, 2, len(m.Am))
	}

	data.Run(t, n, n.Value.(*data.Multi), test)
}
Ejemplo n.º 16
0
func newNonEmptyNode() *node.Node {
	n := node.NewNode()
	n.Parent = node.NewNode()
	n.Array = []*node.Node{node.NewNode(), node.NewNode()}
	n.Map = map[string]*node.Node{"a": node.NewNode(), "b": node.NewNode()}
	n.Key = "a"
	n.Index = 1
	n.Origin = system.NewReference("a", "b")
	n.ValueString = "a"
	n.ValueNumber = 2.0
	n.ValueBool = true
	n.Value = "a"
	n.Val = reflect.ValueOf("a")
	n.Null = false
	n.Missing = false
	n.Rule = &system.RuleWrapper{}
	n.Type = &system.Type{}
	n.JsonType = system.J_OBJECT
	return n
}
Ejemplo n.º 17
0
Archivo: helpers.go Proyecto: kego/ke
func Setup(t *testing.T) (*tests.ContextBuilder, *node.Node) {

	simple := false
	if !simple {
		cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)
		m := `"type": "multi",
			"js": "js1",
			"ss": "ss1",
			"sr": "sr1",
			"jn": 1.1,
			"sn": 1.2,
			"jb": true,
			"sb": false,
			"alajs": ["alajs0", "alajs1", "alajs2"],
			"alas": [{"type": "simple", "js": "alas0"}, {"type": "simple", "js": "alas1"}],
			"alass": ["alass0", "alass1", "alass2"],
			"aljb": true,
			"aljn": 2.0,
			"aljs": "aljs",
			"almjs": {"a": "almjsa", "b": "almjsb"},
			"alms": {"a": {"type": "simple", "js": "almsa"}, "b": {"type": "simple", "js": "almsb"}},
			"almss": {"a": "almssa", "b": "almssb"},
			"alss": "alss",
			"als": {"type": "als", "js": "als"},
			"i": { "type": "facea", "a": "ia" },
			"ajs": [ "ajs0", "ajs1", "ajs2", "ajs3" ],
			"ass": [ "ass0", "ass1", "ass2", "ass3" ],
			"ajn": [ 2.1, 2.2, 2.3, 2.4 ],
			"asn": [ 3.1, 3.2, 3.3, 3.4 ],
			"ajb": [ true, false, false, false ],
			"asb": [ false, true, false, false ],
			"mjs": { "a": "mjsa", "b": "mjsb" },
			"mss": { "a": "mssa", "b": "mssb" },
			"mjn": { "a": 4.1, "b": 4.2 },
			"msn": { "a": 5.1, "b": 5.2 },
			"mjb": { "a": true, "b": false },
			"msb": { "a": false, "b": true },
			"anri": [ "anri0", { "type": "facea", "a": "anri1" }, { "type": "multi", "ss": "anri2" } ],
			"mnri": { "a": "mnria", "b": { "type": "facea", "a": "mnrib" }, "c": { "type": "multi", "ss": "mnric" } }
						`
		mm := m + `,
			"m": { "type": "multi" },
			"am": [ { "type": "multi", "js": "amjs0" }, { "type": "multi", "js": "amjs1" } ],
			"mm": { "a": { "type": "multi", "js": "mmjsa" }, "b": { "type": "multi", "js": "mmjsb" } }`

		s := `{
				` + m + `,
				"m": {` + mm + `},
				"am": [ {` + mm + `}, {` + mm + `} ],
				"mm": { "a": {` + mm + `}, "b": {` + mm + `} }
			}`
		n := node.NewNode()
		require.NoError(t, n.Unpack(cb.Ctx(), system.MustPackString(s), false))
		return cb, n

	} else {
		cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)

		m := `"type": "multi",
		"alms": {"a": {"type": "simple", "js": "almsa"}, "b": {"type": "simple", "js": "almsb"}}
			`
		mm := m + `,
			"m": { "type": "multi" },
			"am": [ { "type": "multi", "js": "amjs0" }, { "type": "multi", "js": "amjs1" } ],
			"mm": { "a": { "type": "multi", "js": "mmjsa" }, "b": { "type": "multi", "js": "mmjsb" } }`

		s := `{
		` + m + `,
		"m": {` + mm + `},
		"am": [ {` + mm + `}, {` + mm + `} ],
		"mm": { "a": {` + mm + `}, "b": {` + mm + `} }
	}`
		n := node.NewNode()
		require.NoError(t, n.Unpack(cb.Ctx(), system.MustPackString(s), false))
		return cb, n
	}

}
Ejemplo n.º 18
0
func TestNode_SetValueZero(t *testing.T) {
	cb, n := data.Setup(t)
	test := func(t *testing.T, n *node.Node, m *data.Multi) {

		sstring, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/system", "string")
		assert.True(t, ok)
		facea, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/tests/data", "facea")
		assert.True(t, ok)
		faceb, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/tests/data", "faceb")
		assert.True(t, ok)

		c1 := node.NewNode()
		require.NoError(t, c1.InitialiseMapItem(cb.Ctx(), n.Map["mss"], "c"))
		require.NoError(t, c1.AddToMap(cb.Ctx(), n.Map["mss"], "c", true))
		require.NoError(t, c1.SetValueZero(cb.Ctx(), true, nil))
		assert.False(t, n.Map["mss"].Map["c"].Missing)
		assert.True(t, n.Map["mss"].Map["c"].Null)
		assert.Nil(t, m.Mss["c"])

		c1a := node.NewNode()
		require.NoError(t, c1a.InitialiseMapItem(cb.Ctx(), n.Map["mss"], "d"))
		require.NoError(t, c1a.AddToMap(cb.Ctx(), n.Map["mss"], "d", true))
		require.NoError(t, c1a.SetValueZero(cb.Ctx(), false, nil))
		assert.False(t, n.Map["mss"].Map["d"].Missing)
		assert.False(t, n.Map["mss"].Map["d"].Null)
		assert.NotNil(t, m.Mss["d"])
		assert.Equal(t, "", m.Mss["d"].Value())

		c2 := node.NewNode()
		require.NoError(t, c2.InitialiseMapItem(cb.Ctx(), n.Map["mm"], "c"))
		require.NoError(t, c2.AddToMap(cb.Ctx(), n.Map["mm"], "c", true))
		require.NoError(t, c2.SetValueZero(cb.Ctx(), true, nil))
		assert.Nil(t, m.Mm["c"])

		c2a := node.NewNode()
		require.NoError(t, c2a.InitialiseMapItem(cb.Ctx(), n.Map["mm"], "d"))
		require.NoError(t, c2a.AddToMap(cb.Ctx(), n.Map["mm"], "d", true))
		require.NoError(t, c2a.SetValueZero(cb.Ctx(), false, nil))
		assert.NotNil(t, m.Mm["d"])
		assert.Equal(t, "", m.Mm["d"].Js)

		require.NoError(t, n.Map["i"].SetValueZero(cb.Ctx(), true, faceb))
		assert.Nil(t, m.I)
		assert.IsType(t, &data.Faceb{}, m.I)

		require.NoError(t, n.Map["sri"].SetValueZero(cb.Ctx(), false, facea))
		assert.NotNil(t, m.Sri)
		assert.IsType(t, &data.Facea{}, m.Sri)

		c3 := node.NewNode()
		require.NoError(t, c3.InitialiseArrayItem(cb.Ctx(), n.Map["ass"], 4))
		require.NoError(t, c3.AddToArray(cb.Ctx(), n.Map["ass"], 4, true))
		require.NoError(t, c3.SetValueZero(cb.Ctx(), true, nil))
		assert.Nil(t, m.Ass[4])

		c3a := node.NewNode()
		require.NoError(t, c3a.InitialiseArrayItem(cb.Ctx(), n.Map["ass"], 5))
		require.NoError(t, c3a.AddToArray(cb.Ctx(), n.Map["ass"], 5, true))
		require.NoError(t, c3a.SetValueZero(cb.Ctx(), false, nil))
		assert.NotNil(t, m.Ass[5])

		c4 := node.NewNode()
		require.NoError(t, c4.InitialiseArrayItem(cb.Ctx(), n.Map["am"], 2))
		require.NoError(t, c4.AddToArray(cb.Ctx(), n.Map["am"], 2, true))
		require.NoError(t, c4.SetValueZero(cb.Ctx(), true, nil))
		assert.Nil(t, m.Am[2])

		c4a := node.NewNode()
		require.NoError(t, c4a.InitialiseArrayItem(cb.Ctx(), n.Map["am"], 3))
		require.NoError(t, c4a.AddToArray(cb.Ctx(), n.Map["am"], 3, true))
		require.NoError(t, c4a.SetValueZero(cb.Ctx(), false, nil))
		assert.NotNil(t, m.Am[3])

		c5 := node.NewNode()
		require.NoError(t, c5.InitialiseArrayItem(cb.Ctx(), n.Map["anri"], 3))
		require.NoError(t, c5.AddToArray(cb.Ctx(), n.Map["anri"], 3, true))
		require.NoError(t, c5.SetValueZero(cb.Ctx(), true, sstring))
		assert.Nil(t, m.Anri[3])
		assert.IsType(t, system.NewString(""), m.Anri[3])

		c5a := node.NewNode()
		require.NoError(t, c5a.InitialiseArrayItem(cb.Ctx(), n.Map["anri"], 4))
		require.NoError(t, c5a.AddToArray(cb.Ctx(), n.Map["anri"], 4, true))
		require.NoError(t, c5a.SetValueZero(cb.Ctx(), false, sstring))
		assert.NotNil(t, m.Anri[4])
		assert.IsType(t, system.NewString(""), m.Anri[4])

		c6 := node.NewNode()
		require.NoError(t, c6.InitialiseArrayItem(cb.Ctx(), n.Map["anri"], 5))
		require.NoError(t, c6.AddToArray(cb.Ctx(), n.Map["anri"], 5, true))
		require.NoError(t, c6.SetValueZero(cb.Ctx(), true, facea))
		assert.Nil(t, m.Anri[5])
		assert.IsType(t, &data.Facea{}, m.Anri[5])

		c6a := node.NewNode()
		require.NoError(t, c6a.InitialiseArrayItem(cb.Ctx(), n.Map["anri"], 6))
		require.NoError(t, c6a.AddToArray(cb.Ctx(), n.Map["anri"], 6, true))
		require.NoError(t, c6a.SetValueZero(cb.Ctx(), false, facea))
		assert.NotNil(t, m.Anri[6])
		assert.IsType(t, &data.Facea{}, m.Anri[6])

		c7 := node.NewNode()
		require.NoError(t, c7.InitialiseMapItem(cb.Ctx(), n.Map["mnri"], "d"))
		require.NoError(t, c7.AddToMap(cb.Ctx(), n.Map["mnri"], "d", true))
		require.NoError(t, c7.SetValueZero(cb.Ctx(), true, sstring))
		assert.Nil(t, m.Mnri["d"])
		assert.IsType(t, system.NewString(""), m.Mnri["d"])

		c7a := node.NewNode()
		require.NoError(t, c7a.InitialiseMapItem(cb.Ctx(), n.Map["mnri"], "e"))
		require.NoError(t, c7a.AddToMap(cb.Ctx(), n.Map["mnri"], "e", true))
		require.NoError(t, c7a.SetValueZero(cb.Ctx(), false, sstring))
		assert.NotNil(t, m.Mnri["e"])
		assert.IsType(t, system.NewString(""), m.Mnri["e"])

		c8 := node.NewNode()
		require.NoError(t, c8.InitialiseMapItem(cb.Ctx(), n.Map["mnri"], "f"))
		require.NoError(t, c8.AddToMap(cb.Ctx(), n.Map["mnri"], "f", true))
		require.NoError(t, c8.SetValueZero(cb.Ctx(), true, facea))
		assert.Nil(t, m.Mnri["f"])
		assert.IsType(t, &data.Facea{}, m.Mnri["f"])

		c8a := node.NewNode()
		require.NoError(t, c8a.InitialiseMapItem(cb.Ctx(), n.Map["mnri"], "g"))
		require.NoError(t, c8a.AddToMap(cb.Ctx(), n.Map["mnri"], "g", true))
		require.NoError(t, c8a.SetValueZero(cb.Ctx(), false, facea))
		assert.NotNil(t, m.Mnri["g"])
		assert.IsType(t, &data.Facea{}, m.Mnri["g"])
	}
	data.Run(t, n, n.Value.(*data.Multi), test)

}
Ejemplo n.º 19
0
Archivo: addpop.go Proyecto: kego/ke
func (v *AddPopupView) save() {

	var t *system.Type
	if len(v.model.Types) == 1 {
		t = v.model.Types[0]
	} else {
		options := v.typeSelect.Node.Get("options")
		selectedIndex := v.typeSelect.Node.Get("selectedIndex").Int()
		value := options.Index(selectedIndex).Get("id").String()
		if value == "" {
			return
		}
		r, err := system.NewReferenceFromString(v.Ctx, value)
		if err != nil {
			v.App.Fail <- kerr.Wrap("SEMCIELKRN", err)
			return
		}
		ty, ok := system.GetTypeFromCache(v.Ctx, r.Package, r.Name)
		if !ok {
			v.App.Fail <- kerr.New("RWHSCOFNQM", "Type %s not found in cache", r.Value())
			return
		}
		t = ty
	}

	switch {
	case v.model.IsMap():
		key := v.nameInput.Node.Get("value").String()
		if key == "" {
			// TODO: show an error
			return
		}
		if _, duplicate := v.model.Parent.Map[key]; duplicate {
			// TODO: show an error
			return
		}
		v.App.Dispatch(&actions.Add{
			Undoer: &actions.Undoer{},
			Node:   node.NewNode(),
			Parent: v.model.Parent,
			Key:    key,
			Type:   t,
		})
	case v.model.IsArray():
		v.App.Dispatch(&actions.Add{
			Undoer: &actions.Undoer{},
			Node:   node.NewNode(),
			Parent: v.model.Parent,
			Index:  len(v.model.Parent.Array),
			Type:   t,
		})
	case v.model.IsField():
		v.App.Dispatch(&actions.Add{
			Undoer: &actions.Undoer{},
			Parent: v.model.Parent,
			Node:   v.model.Node,
			Key:    v.model.Node.Key,
			Type:   t,
		})
	case v.model.IsFile():
		name := v.nameInput.Node.Get("value").String()
		if name == "" {
			// TODO: show an error
			return
		}
		// TODO: show an error if a duplicate file exists
		v.App.Dispatch(&actions.Add{
			Undoer:     &actions.Undoer{},
			Node:       node.NewNode(),
			Type:       t,
			BranchName: name,
			BranchFile: name + ".json", // TODO: choose filename based on package
		})
	}

	v.App.Dispatch(&actions.CloseAddPopup{})

}
Ejemplo n.º 20
0
func TestDeleteMutation(t *testing.T) {

	cb, n := data.Setup(t)

	test := func(t *testing.T, n *node.Node, m *data.Multi) {
		var b, d, p *node.Node

		b = node.NewNode()
		d = n.Map["jb"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["jb"].ValueBool)
		assert.False(t, m.Jb)
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["jb"].ValueBool)
		assert.True(t, m.Jb)

		b = node.NewNode()
		d = n.Map["ss"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["ss"].Missing)
		assert.Equal(t, "", n.Map["ss"].ValueString)
		assert.Nil(t, m.Ss)
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["ss"].Missing)
		assert.Equal(t, "ss1", n.Map["ss"].ValueString)
		require.NotNil(t, m.Ss)
		assert.Equal(t, "ss1", m.Ss.Value())

		b = node.NewNode()
		d = n.Map["i"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["i"].Missing)
		assert.Nil(t, m.I)
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["i"].Missing)
		assert.Equal(t, "ia", n.Map["i"].Value.(*data.Facea).A.Value())
		require.NotNil(t, m.I)
		assert.Equal(t, "ia", m.I.Face())

		b = node.NewNode()
		d = n.Map["ass"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["ass"].Missing)
		assert.Equal(t, 0, len(m.Ass))
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["ass"].Missing)
		assert.Equal(t, 4, len(m.Ass))

		b = node.NewNode()
		d = n.Map["mss"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["mss"].Missing)
		assert.Nil(t, m.Mss)
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["mss"].Missing)
		require.NotNil(t, m.Mss)
		assert.Equal(t, 2, len(m.Mss))
		require.NotNil(t, m.Mss["a"])
		assert.Equal(t, "mssa", m.Mss["a"].Value())

		b = node.NewNode()
		d = n.Map["ass"].Array[0]
		p = n.Map["ass"]
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.Equal(t, 3, len(n.Map["ass"].Array))
		assert.Equal(t, 3, len(m.Ass))
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.Equal(t, 4, len(n.Map["ass"].Array))
		assert.Equal(t, 4, len(m.Ass))
		assert.Equal(t, "ass0", n.Map["ass"].Array[0].ValueString)
		require.NotNil(t, m.Ass[0])
		assert.Equal(t, "ass0", m.Ass[0].Value())

	}

	data.Run(t, n, n.Value.(*data.Multi), test)
}
Ejemplo n.º 21
0
Archivo: node.go Proyecto: kego/ke
func (s *NodeStore) Handle(payload *flux.Payload) bool {
	switch action := payload.Action.(type) {
	case *actions.ToggleSystemControls:
		m := s.Get(action.Node)
		m.ShowSystemControls = !m.ShowSystemControls
		payload.Notify(action.Node, NodeSystemControls)
	case *actions.Add:
		payload.Wait(s.app.Actions)
		switch action.Direction() {
		case actions.New:
			if err := mutateAddNode(s.ctx, action.Node, action.Parent, action.Key, action.Index, action.Type, action.BranchName); err != nil {
				s.app.Fail <- kerr.Wrap("HUOGBUQCAO", err)
				break
			}
			payload.Notify(action.Node, NodeInitialised)
			if action.Parent != nil {
				payload.Notify(action.Parent, NodeChildAdded)
			}
			payload.Notify(action.Node, NodeFocus)
		case actions.Undo:
			action.Backup = node.NewNode()
			if err := mutateDeleteNode(s.ctx, action.Node, action.Parent, action.Backup); err != nil {
				s.app.Fail <- kerr.Wrap("RTAGMUIKMD", err)
				break
			}
			payload.Notify(action.Node, NodeDeleted)
			if action.Parent != nil {
				payload.Notify(action.Parent, NodeChildDeleted)
			}
		case actions.Redo:
			if err := mutateRestoreNode(s.ctx, action.Node, action.Parent, action.Backup); err != nil {
				s.app.Fail <- kerr.Wrap("MHUTMXOGBP", err)
				break
			}
			payload.Notify(action.Node, NodeInitialised)
			payload.Notify(action.Parent, NodeChildAdded)
			payload.Notify(action.Node, NodeFocus)
		}

		if action.Parent != nil {
			model := s.app.Nodes.Get(action.Parent)
			changed, err := model.Validate(s.ctx, s.app.Rule.Get(action.Parent.Root(), action.Parent))
			if err != nil {
				s.app.Fail <- kerr.Wrap("AKIUPRWGLV", err)
				break
			}
			if changed {
				payload.Notify(action.Parent, NodeErrorsChanged)
			}
		}

		c := action.Parent
		for c != nil {
			payload.Notify(c, NodeDescendantChanged)
			c = c.Parent
		}
	case *actions.Delete:
		payload.Wait(s.app.Actions)
		switch action.Direction() {
		case actions.New, actions.Redo:
			action.Backup = node.NewNode()
			if err := mutateDeleteNode(s.ctx, action.Node, action.Parent, action.Backup); err != nil {
				s.app.Fail <- kerr.Wrap("DFHTKJRLQC", err)
				break
			}
			payload.Notify(action.Node, NodeDeleted)
			payload.Notify(action.Parent, NodeChildDeleted)
		case actions.Undo:
			if err := mutateRestoreNode(s.ctx, action.Node, action.Parent, action.Backup); err != nil {
				s.app.Fail <- kerr.Wrap("HAPWUOPBTW", err)
				break
			}
			payload.Notify(action.Node, NodeInitialised)
			payload.Notify(action.Parent, NodeChildAdded)
			payload.Notify(action.Node, NodeFocus)
		}

		if action.Parent != nil {
			model := s.app.Nodes.Get(action.Parent)
			changed, err := model.Validate(s.ctx, s.app.Rule.Get(action.Parent.Root(), action.Parent))
			if err != nil {
				s.app.Fail <- kerr.Wrap("IOFCSITVXB", err)
				break
			}
			if changed {
				payload.Notify(action.Parent, NodeErrorsChanged)
			}
		}

		c := action.Parent
		for c != nil {
			payload.Notify(c, NodeDescendantChanged)
			c = c.Parent
		}
	case *actions.Reorder:
		payload.Wait(s.app.Actions)
		if !action.Model.Node.Type.IsNativeArray() {
			s.app.Fail <- kerr.New("EPBQVIICFM", "Must be array")
			break
		}
		from := action.Before
		to := action.After
		if action.Direction() == actions.Undo {
			from = action.After
			to = action.Before
		}
		if err := action.Model.Node.ReorderArrayChild(from, to); err != nil {
			s.app.Fail <- kerr.Wrap("HMIFPKVJCN", err)
			break
		}
		payload.Notify(action.Model.Node, NodeArrayReorder)
		c := action.Model.Node.Parent
		for c != nil {
			payload.Notify(c, NodeDescendantChanged)
			c = c.Parent
		}

	case *actions.Modify:
		payload.Wait(s.app.Actions)
		n := action.Editor.Node
		val := action.After
		if action.Direction() == actions.Undo {
			val = action.Before
		}
		if err := n.SetValue(s.ctx, val); err != nil {
			s.app.Fail <- kerr.Wrap("VIMXVIHPFY", err)
			break
		}
		model := s.app.Nodes.Get(n)
		changed, err := model.Validate(s.ctx, s.app.Rule.Get(n.Root(), n))
		if err != nil {
			s.app.Fail <- kerr.Wrap("EEIYMGQCCA", err)
			break
		}
		if changed {
			payload.Notify(n, NodeErrorsChanged)
		}

		payload.Notify(n, NodeValueChanged)

		c := n.Parent
		for c != nil {
			payload.Notify(c, NodeDescendantChanged)
			c = c.Parent
		}
	case *actions.EditorFocus:
		payload.Notify(action.Editor.Node, NodeFocus)
	case *actions.InitialState:
		payload.Wait(s.app.Package, s.app.Types)
		n := s.app.Package.Node()
		if err := n.RecomputeHash(s.ctx, true); err != nil {
			s.app.Fail <- kerr.Wrap("NDMDVGISWR", err)
		}
		for _, ti := range s.app.Types.All() {
			if err := ti.Node.RecomputeHash(s.ctx, true); err != nil {
				s.app.Fail <- kerr.Wrap("YLRDBXIYJH", err)
			}
		}
	case *actions.LoadFileSuccess:
		nci, ok := action.Branch.Contents.(models.NodeContentsInterface)
		if !ok {
			break
		}
		if err := nci.GetNode().RecomputeHash(s.ctx, true); err != nil {
			s.app.Fail <- kerr.Wrap("BWUPWAFALG", err)
		}
	}

	if m, ok := payload.Action.(actions.Mutator); ok {
		n := m.CommonAncestor()
		c := n
		for c != nil {
			// For the actual node, we recompute the whole hash. For ancestors,
			// we recompute the hash without recomputing child nodes.
			recomputeChildren := c == n
			if err := c.RecomputeHash(s.ctx, recomputeChildren); err != nil {
				s.app.Fail <- kerr.Wrap("SWHIXHLHXM", err)
			}
			c = c.Parent
		}
	}

	return true
}