Esempio n. 1
0
func makeCommitType(valueType *types.Type, parentsValueTypes []*types.Type, metaType *types.Type, parentsMetaTypes []*types.Type) *types.Type {
	tmp := make([]*types.Type, len(parentsValueTypes), len(parentsValueTypes)+1)
	copy(tmp, parentsValueTypes)
	tmp = append(tmp, valueType)
	parentsValueUnionType := types.MakeUnionType(tmp...)

	tmp2 := make([]*types.Type, len(parentsMetaTypes), len(parentsMetaTypes)+1)
	copy(tmp2, parentsMetaTypes)
	tmp2 = append(tmp2, metaType)
	parentsMetaUnionType := types.MakeUnionType(tmp2...)

	fieldNames := []string{MetaField, ParentsField, ValueField}
	var parentsType *types.Type
	if parentsValueUnionType.Equals(valueType) && parentsMetaUnionType.Equals(metaType) {
		parentsType = types.MakeSetType(types.MakeRefType(types.MakeCycleType(0)))
	} else {
		parentsType = types.MakeSetType(types.MakeRefType(
			types.MakeStructType("Commit", fieldNames, []*types.Type{
				parentsMetaUnionType,
				types.MakeSetType(types.MakeRefType(types.MakeCycleType(0))),
				parentsValueUnionType,
			})))
	}
	fieldTypes := []*types.Type{
		metaType,
		parentsType,
		valueType,
	}

	return types.MakeStructType("Commit", fieldNames, fieldTypes)
}
Esempio n. 2
0
func (suite *ParsedResultTestSuite) SetupTest() {
	suite.prim = newTestField("a", types.NumberType, "")
	suite.prim2 = newTestField("b", types.StringType, "")
	suite.compound = newTestField("set", types.MakeSetType(types.StringType), "")
	suite.compoundOfCompound = newTestField("listOfSet", types.MakeListType(types.MakeSetType(types.StringType)), "")
	suite.namedType = newTestField("otherStruct", makeUnresolvedType("", "Other"), "Other")
	suite.namespacedType = newTestField("namespacedStruct", makeUnresolvedType("Elsewhere", "Other"), "Elsewhere.Other")
	suite.mapOfNamedType = newTestField("mapOfStructToOther", types.MakeMapType(makeUnresolvedType("", "Struct"), makeUnresolvedType("Elsewhere", "Other")), "Map<Struct, Elsewhere.Other>")
}
Esempio n. 3
0
func TestNewCommit(t *testing.T) {
	assert := assert.New(t)

	commitFieldNames := []string{MetaField, ParentsField, ValueField}

	assertTypeEquals := func(e, a *types.Type) {
		assert.True(a.Equals(e), "Actual: %s\nExpected %s", a.Describe(), e.Describe())
	}

	commit := NewCommit(types.Number(1), types.NewSet(), types.EmptyStruct)
	at := commit.Type()
	et := types.MakeStructType("Commit", commitFieldNames, []*types.Type{
		types.EmptyStructType,
		types.MakeSetType(types.MakeRefType(types.MakeCycleType(0))),
		types.NumberType,
	})
	assertTypeEquals(et, at)

	// Commiting another Number
	commit2 := NewCommit(types.Number(2), types.NewSet(types.NewRef(commit)), types.EmptyStruct)
	at2 := commit2.Type()
	et2 := et
	assertTypeEquals(et2, at2)

	// Now commit a String
	commit3 := NewCommit(types.String("Hi"), types.NewSet(types.NewRef(commit2)), types.EmptyStruct)
	at3 := commit3.Type()
	et3 := types.MakeStructType("Commit", commitFieldNames, []*types.Type{
		types.EmptyStructType,
		types.MakeSetType(types.MakeRefType(types.MakeStructType("Commit", commitFieldNames, []*types.Type{
			types.EmptyStructType,
			types.MakeSetType(types.MakeRefType(types.MakeCycleType(0))),
			types.MakeUnionType(types.NumberType, types.StringType),
		}))),
		types.StringType,
	})
	assertTypeEquals(et3, at3)

	// Now commit a String with MetaInfo
	meta := types.NewStruct("Meta", types.StructData{"date": types.String("some date"), "number": types.Number(9)})
	metaType := types.MakeStructType("Meta", []string{"date", "number"}, []*types.Type{types.StringType, types.NumberType})
	assertTypeEquals(metaType, meta.Type())
	commit4 := NewCommit(types.String("Hi"), types.NewSet(types.NewRef(commit2)), meta)
	at4 := commit4.Type()
	et4 := types.MakeStructType("Commit", commitFieldNames, []*types.Type{
		metaType,
		types.MakeSetType(types.MakeRefType(types.MakeStructType("Commit", commitFieldNames, []*types.Type{
			types.MakeUnionType(types.EmptyStructType, metaType),
			types.MakeSetType(types.MakeRefType(types.MakeCycleType(0))),
			types.MakeUnionType(types.NumberType, types.StringType),
		}))),
		types.StringType,
	})
	assertTypeEquals(et4, at4)
}
Esempio n. 4
0
func (suite *DatabaseSuite) TestDatabaseHeightOfCollections() {
	setOfStringType := types.MakeSetType(types.StringType)
	setOfRefOfStringType := types.MakeSetType(types.MakeRefType(types.StringType))

	// Set<String>
	v1 := types.String("hello")
	v2 := types.String("world")
	s1 := types.NewSet(v1, v2)
	suite.Equal(uint64(1), suite.ds.WriteValue(s1).Height())

	// Set<Ref<String>>
	s2 := types.NewSet(suite.ds.WriteValue(v1), suite.ds.WriteValue(v2))
	suite.Equal(uint64(2), suite.ds.WriteValue(s2).Height())

	// List<Set<String>>
	v3 := types.String("foo")
	v4 := types.String("bar")
	s3 := types.NewSet(v3, v4)
	l1 := types.NewList(s1, s3)
	suite.Equal(uint64(1), suite.ds.WriteValue(l1).Height())

	// List<Ref<Set<String>>
	l2 := types.NewList(suite.ds.WriteValue(s1), suite.ds.WriteValue(s3))
	suite.Equal(uint64(2), suite.ds.WriteValue(l2).Height())

	// List<Ref<Set<Ref<String>>>
	s4 := types.NewSet(suite.ds.WriteValue(v3), suite.ds.WriteValue(v4))
	l3 := types.NewList(suite.ds.WriteValue(s4))
	suite.Equal(uint64(3), suite.ds.WriteValue(l3).Height())

	// List<Set<String> | RefValue<Set<String>>>
	l4 := types.NewList(s1, suite.ds.WriteValue(s3))
	suite.Equal(uint64(2), suite.ds.WriteValue(l4).Height())
	l5 := types.NewList(suite.ds.WriteValue(s1), s3)
	suite.Equal(uint64(2), suite.ds.WriteValue(l5).Height())

	// Familiar with the "New Jersey Turnpike" drink? Here's the noms version of that...
	everything := []types.Value{v1, v2, s1, s2, v3, v4, s3, l1, l2, s4, l3, l4, l5}
	andMore := make([]types.Value, 0, len(everything)*3+2)
	for _, v := range everything {
		andMore = append(andMore, v, v.Type(), suite.ds.WriteValue(v))
	}
	andMore = append(andMore, setOfStringType, setOfRefOfStringType)

	suite.ds.WriteValue(types.NewList(andMore...))
}
Esempio n. 5
0
func init() {
	structName := "Commit"

	// struct Commit {
	//   parents: Set<Ref<Commit>>
	//   value: Value
	// }

	fieldTypes := types.TypeMap{
		ParentsField: nil,
		ValueField:   types.ValueType,
	}
	commitType = types.MakeStructType(structName, fieldTypes)
	commitType.Desc.(types.StructDesc).SetField(ParentsField, types.MakeSetType(types.MakeRefType(commitType)))
	refOfCommitType = types.MakeRefType(commitType)
}
Esempio n. 6
0
func TestNomsTypeDiff(t *testing.T) {
	assert := assert.New(t)

	expected := "-   List<Number>\n+   List<String>\n"
	t1 := types.MakeListType(types.NumberType)
	t2 := types.MakeListType(types.StringType)
	buf := util.NewBuffer(nil)
	Diff(buf, t1, t2)
	assert.Equal(expected, buf.String())

	expected = "-   List<Number>\n+   Set<String>\n"
	t1 = types.MakeListType(types.NumberType)
	t2 = types.MakeSetType(types.StringType)
	buf = util.NewBuffer(nil)
	Diff(buf, t1, t2)
	assert.Equal(expected, buf.String())
}
Esempio n. 7
0
// resolveReferences replaces references with the actual Type
func resolveReferences(i *intermediate, aliases map[string][]*types.Type) {
	var rec func(t *types.Type) *types.Type
	resolveFields := func(desc types.StructDesc) *types.Type {
		fields := make(types.TypeMap, desc.Len())
		desc.IterFields(func(name string, t *types.Type) {
			fields[name] = rec(t)
		})
		return types.MakeStructType(desc.Name, fields)
	}
	rec = func(t *types.Type) *types.Type {
		switch t.Kind() {
		case UnresolvedKind:
			desc := t.Desc.(UnresolvedDesc)
			if desc.Namespace == "" {
				return findType(desc.Name, i.Types)
			}
			ts, ok := aliases[desc.Namespace]
			d.Exp.True(ok, "No such namespace: %s", desc.Namespace)
			return findType(desc.Name, ts)
		case types.ListKind:
			return types.MakeListType(rec(t.Desc.(types.CompoundDesc).ElemTypes[0]))
		case types.SetKind:
			return types.MakeSetType(rec(t.Desc.(types.CompoundDesc).ElemTypes[0]))
		case types.RefKind:
			return types.MakeRefType(rec(t.Desc.(types.CompoundDesc).ElemTypes[0]))
		case types.MapKind:
			elemTypes := t.Desc.(types.CompoundDesc).ElemTypes
			return types.MakeMapType(rec(elemTypes[0]), rec(elemTypes[1]))
		case types.StructKind:
			return resolveFields(t.Desc.(types.StructDesc))

		}
		return t
	}

	for idx, t := range i.Types {
		i.Types[idx] = rec(t)
	}
}
Esempio n. 8
0
func typeForSetOfRefOfCommit() *types.Type {
	return types.MakeSetType(refOfCommitType)
}
Esempio n. 9
0
func (c *current) onCompoundType29(t interface{}) (interface{}, error) {
	return types.MakeSetType(t.(*types.Type)), nil
}