Ejemplo n.º 1
0
// This function builds up a Noms value that describes the type
// package implemented by this file and registers it with the global
// type package definition cache.
func init() {
	p := types.NewPackage([]types.Type{
		types.MakeEnumType("QuarterEnum", "Q1", "Q2", "Q3", "Q4"),
		types.MakeStructType("Quarter",
			[]types.Field{
				types.Field{"Year", types.MakePrimitiveType(types.Int32Kind), false},
				types.Field{"Quarter", types.MakeType(ref.Ref{}, 0), false},
			},
			types.Choices{},
		),
		types.MakeStructType("Key",
			[]types.Field{},
			types.Choices{
				types.Field{"Category", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"Quarter", types.MakeType(ref.Ref{}, 1), false},
				types.Field{"Region", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"RoundType", types.MakeType(ref.Ref{}, 3), false},
				types.Field{"Year", types.MakePrimitiveType(types.Int32Kind), false},
			},
		),
		types.MakeEnumType("RoundTypeEnum", "Seed", "SeriesA", "SeriesB", "SeriesC", "SeriesD", "SeriesE", "SeriesF", "SeriesG", "SeriesH", "UnknownRoundType"),
	}, []ref.Ref{
		ref.Parse("sha1-6c64b08a509e25f9814dbf036489267c957a6fd8"),
		ref.Parse("sha1-91ae65b19b4817fc15d4e2c5c7472c68b4950b77"),
	})
	__mainPackageInFile_index_CachedRef = types.RegisterPackage(&p)
}
Ejemplo n.º 2
0
func TestGenerateDeps(t *testing.T) {
	assert := assert.New(t)
	ds := datas.NewDataStore(chunks.NewMemoryStore())
	dir, err := ioutil.TempDir("", "codegen_test_")
	assert.NoError(err)
	defer os.RemoveAll(dir)

	leaf1 := types.NewPackage([]types.Type{types.MakeEnumType("e1", "a", "b")}, []ref.Ref{})
	leaf1Ref := ds.WriteValue(leaf1).TargetRef()
	leaf2 := types.NewPackage([]types.Type{types.MakePrimitiveType(types.BoolKind)}, []ref.Ref{})
	leaf2Ref := ds.WriteValue(leaf2).TargetRef()

	depender := types.NewPackage([]types.Type{}, []ref.Ref{leaf1Ref})
	dependerRef := ds.WriteValue(depender).TargetRef()

	top := types.NewPackage([]types.Type{}, []ref.Ref{leaf2Ref, dependerRef})
	types.RegisterPackage(&top)

	localPkgs := refSet{top.Ref(): true}
	generateDepCode(filepath.Base(dir), dir, map[string]bool{}, top, localPkgs, ds)

	leaf1Path := filepath.Join(dir, code.ToTag(leaf1.Ref())+".go")
	leaf2Path := filepath.Join(dir, code.ToTag(leaf2.Ref())+".go")
	leaf3Path := filepath.Join(dir, code.ToTag(depender.Ref())+".go")
	_, err = os.Stat(leaf1Path)
	assert.NoError(err)
	_, err = os.Stat(leaf2Path)
	assert.NoError(err)
	_, err = os.Stat(leaf3Path)
	assert.NoError(err)
}
Ejemplo n.º 3
0
func TestSkipDuplicateTypes(t *testing.T) {
	assert := assert.New(t)
	dir, err := ioutil.TempDir("", "codegen_test_")
	assert.NoError(err)
	defer os.RemoveAll(dir)

	leaf1 := types.NewPackage([]types.Type{
		types.MakeEnumType("E1", "a", "b"),
		types.MakeStructType("S1", []types.Field{
			types.Field{"f", types.MakeCompoundType(types.ListKind, types.MakePrimitiveType(types.Uint16Kind)), false},
			types.Field{"e", types.MakeType(ref.Ref{}, 0), false},
		}, types.Choices{}),
	}, []ref.Ref{})
	leaf2 := types.NewPackage([]types.Type{
		types.MakeStructType("S2", []types.Field{
			types.Field{"f", types.MakeCompoundType(types.ListKind, types.MakePrimitiveType(types.Uint16Kind)), false},
		}, types.Choices{}),
	}, []ref.Ref{})

	written := map[string]bool{}
	tag1 := code.ToTag(leaf1.Ref())
	leaf1Path := filepath.Join(dir, tag1+".go")
	generateAndEmit(tag1, leaf1Path, written, depsMap{}, pkg.Parsed{Package: leaf1, Name: "p"})

	tag2 := code.ToTag(leaf2.Ref())
	leaf2Path := filepath.Join(dir, tag2+".go")
	generateAndEmit(tag2, leaf2Path, written, depsMap{}, pkg.Parsed{Package: leaf2, Name: "p"})

	code, err := ioutil.ReadFile(leaf2Path)
	assert.NoError(err)
	assert.NotContains(string(code), "type ListOfUint16")
}
Ejemplo n.º 4
0
// This function builds up a Noms value that describes the type
// package implemented by this file and registers it with the global
// type package definition cache.
func init() {
	p := types.NewPackage([]types.Type{
		types.MakeEnumType("Handedness", "right", "left", "switch"),
		types.MakeStructType("EnumStruct",
			[]types.Field{
				types.Field{"hand", types.MakeType(ref.Ref{}, 0), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{})
	__genPackageInFile_enum_struct_CachedRef = types.RegisterPackage(&p)
}
Ejemplo n.º 5
0
// This function builds up a Noms value that describes the type
// package implemented by this file and registers it with the global
// type package definition cache.
func init() {
	p := types.NewPackage([]types.Type{
		types.MakeStructType("S",
			[]types.Field{
				types.Field{"s", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"b", types.MakePrimitiveType(types.BoolKind), false},
			},
			types.Choices{},
		),
		types.MakeEnumType("E", "e1", "e2", "e3"),
	}, []ref.Ref{})
	__leafDepPackageInFile_leafDep_CachedRef = types.RegisterPackage(&p)
}
Ejemplo n.º 6
0
// This function builds up a Noms value that describes the type
// package implemented by this file and registers it with the global
// type package definition cache.
func init() {
	p := types.NewPackage([]types.Type{
		types.MakeEnumType("LocalE", "LocalE1", "Ignored"),
		types.MakeStructType("ImportUser",
			[]types.Field{
				types.Field{"importedStruct", types.MakeType(ref.Parse("sha1-eda4273cba9d5d4a1bccf41bcaec64743863cde0"), 0), false},
				types.Field{"enum", types.MakeType(ref.Ref{}, 0), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{
		ref.Parse("sha1-eda4273cba9d5d4a1bccf41bcaec64743863cde0"),
	})
	__genPackageInFile_struct_with_imports_CachedRef = types.RegisterPackage(&p)
}
Ejemplo n.º 7
0
func (suite *ImportTestSuite) SetupTest() {
	suite.vrw = datas.NewDataStore(chunks.NewMemoryStore())

	ns := types.MakeStructType("NestedDepStruct", []types.Field{}, types.Choices{
		types.Field{"b", types.MakePrimitiveType(types.BoolKind), false},
		types.Field{"i", types.MakePrimitiveType(types.Int8Kind), false},
	})
	suite.nested = types.NewPackage([]types.Type{ns}, []ref.Ref{})
	suite.nestedRef = suite.vrw.WriteValue(suite.nested).TargetRef()

	fs := types.MakeStructType("ForeignStruct", []types.Field{
		types.Field{"b", types.MakeType(ref.Ref{}, 1), false},
		types.Field{"n", types.MakeType(suite.nestedRef, 0), false},
	},
		types.Choices{})
	fe := types.MakeEnumType("ForeignEnum", "uno", "dos")
	suite.imported = types.NewPackage([]types.Type{fs, fe}, []ref.Ref{suite.nestedRef})
	suite.importRef = suite.vrw.WriteValue(suite.imported).TargetRef()
}
Ejemplo n.º 8
0
func TestUserName(t *testing.T) {
	assert := assert.New(t)

	imported := types.NewPackage([]types.Type{
		types.MakeEnumType("E1", "a", "b"),
		types.MakeStructType("S1", []types.Field{
			types.Field{"f", types.MakePrimitiveType(types.BoolKind), false},
		}, types.Choices{}),
	}, []ref.Ref{})

	res := testResolver{assert, map[ref.Ref]types.Package{imported.Ref(): imported}}

	localStructName := "Local"
	resolved := types.MakeStructType(localStructName, []types.Field{
		types.Field{"a", types.MakePrimitiveType(types.Int8Kind), false},
	}, types.Choices{})

	g := Generator{R: &res}
	assert.Equal(localStructName, g.UserName(resolved))

	listOfImported := types.MakeCompoundType(types.ListKind, types.MakeType(imported.Ref(), 1))
	assert.Equal(fmt.Sprintf("ListOfS1"), g.UserName(listOfImported))
}