Ejemplo n.º 1
0
func (a Alajs) GetString(ctx context.Context) *system.String {
	out := ""
	for _, v := range a {
		out += v
	}
	return system.NewString(out)
}
Ejemplo n.º 2
0
func TestRuleWrapper_ZeroValue(t *testing.T) {
	cb := tests.Context("kego.io/system").Jauto().Sauto(parser.Parse)
	r := system.WrapRule(cb.Ctx(), &system.MapRule{
		Object: &system.Object{Type: system.NewReference("kego.io/system", "@map")},
		Rule:   &system.Rule{},
		Items: &system.StringRule{
			Object: &system.Object{Type: system.NewReference("kego.io/system", "@string")},
			Rule:   &system.Rule{},
		},
	})

	v, err := r.ZeroValue(true)
	require.NoError(t, err)
	assert.IsType(t, map[string]*system.String{}, v.Interface())
	assert.Nil(t, v.Interface())

	v, err = r.ZeroValue(false)
	require.NoError(t, err)
	assert.IsType(t, map[string]*system.String{}, v.Interface())
	assert.NotNil(t, v.Interface())
	vv := v.Interface().(map[string]*system.String)
	vv["a"] = system.NewString("")

	r = system.WrapRule(cb.Ctx(), &system.MapRule{
		Object: &system.Object{Type: system.NewReference("kego.io/system", "@array")},
		Rule:   &system.Rule{},
		Items: &system.StringRule{
			Object: &system.Object{Type: system.NewReference("kego.io/system", "@string")},
			Rule:   &system.Rule{},
		},
	})

	v, err = r.ZeroValue(true)
	require.NoError(t, err)
	assert.IsType(t, []*system.String{}, v.Interface())
	assert.Nil(t, v.Interface())

	v, err = r.ZeroValue(false)
	require.NoError(t, err)
	assert.IsType(t, []*system.String{}, v.Interface())
	assert.NotNil(t, v.Interface())
	va := v.Interface().([]*system.String)
	va = append(va, system.NewString(""))
}
Ejemplo n.º 3
0
func (m Almjs) GetString(ctx context.Context) *system.String {
	var keys []string
	for k := range m {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	out := ""
	for _, k := range keys {
		out += k
		out += m[k]
	}
	return system.NewString(out)
}
Ejemplo n.º 4
0
Archivo: parser.go Proyecto: kego/ke
func ProcessTypeFileBytes(ctx context.Context, env *envctx.Env, filename string, bytes []byte, cache *sysctx.SysPackageInfo, hash *PackageHasher) error {
	t := new(system.Type)
	if err := system.Unmarshal(envctx.NewContext(ctx, env), bytes, t); err != nil {
		return kerr.Wrap("NLRRVIDVWM", err)
	}
	if hash != nil {
		hash.Types[t.Id.Name] = cityhash.CityHash64(bytes, uint32(len(bytes)))
	}
	cache.Types.Set(t.Id.Name, filename, t)
	cache.Files.Set(t.Id.Name, filename, bytes)
	if t.Rule != nil {
		id := system.NewReference(t.Id.Package, fmt.Sprint("@", t.Id.Name))
		if t.Rule.Id != nil && *t.Rule.Id != *id {
			return kerr.New("JKARKEDTIW", "Incorrect id for %v - it should be %v", t.Rule.Id.String(), id.String())
		}
		t.Rule.Id = id

		// Check that the rule embeds system:rule
		found := false
		for _, em := range t.Rule.Embed {
			if *em == *system.NewReference("kego.io/system", "rule") {
				found = true
			}
		}
		if !found {
			return kerr.New("LMALEMKFDI", "%s does not embed system:rule", id.String())
		}

		cache.Types.Set(id.Name, filename, t.Rule)
	} else {
		// If the rule is missing, automatically create a default.
		id := system.NewReference(t.Id.Package, fmt.Sprint("@", t.Id.Name))
		rule := &system.Type{
			Object: &system.Object{
				Description: fmt.Sprintf("Automatically created basic rule for %s", t.Id.Name),
				Type:        system.NewReference("kego.io/system", "type"),
				Id:          id,
			},
			Embed:     []*system.Reference{system.NewReference("kego.io/system", "rule")},
			Native:    system.NewString("object"),
			Interface: false,
		}
		cache.Types.Set(id.Name, filename, rule)
	}

	return nil
}
Ejemplo n.º 5
0
Archivo: node_test.go Proyecto: kego/ke
func TestNode_SetValueZero3(t *testing.T) {

	n := NewNode()
	err := n.setZero(context.Background(), false, true)
	assert.IsError(t, err, "NYQULBBBHO")

	n = NewNode()
	err = n.setZero(context.Background(), true, true)
	assert.IsError(t, err, "XRYLQWRNPH")

	n = NewNode()
	err = n.setZero(context.Background(), true, false)
	assert.IsError(t, err, "ABXFQOYCBA")

	n = NewNode()
	n.Type = &system.Type{Native: system.NewString("map")}
	err = n.setZero(context.Background(), false, false)
	assert.IsError(t, err, "VGKTIRMDTJ")

}
Ejemplo n.º 6
0
func (s *Aljs2) GetString(ctx context.Context) *system.String {
	return system.NewString(string(*s))
}
Ejemplo n.º 7
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)

}