示例#1
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")
}
示例#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)
}
示例#3
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)
}
示例#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.MakeStructType("StructWithUnions",
			[]types.Field{
				types.Field{"a", types.MakeType(ref.Ref{}, 1), false},
				types.Field{"d", types.MakeType(ref.Ref{}, 2), false},
			},
			types.Choices{},
		),
		types.MakeStructType("",
			[]types.Field{},
			types.Choices{
				types.Field{"b", types.MakePrimitiveType(types.Float64Kind), false},
				types.Field{"c", types.MakePrimitiveType(types.StringKind), false},
			},
		),
		types.MakeStructType("",
			[]types.Field{},
			types.Choices{
				types.Field{"e", types.MakePrimitiveType(types.Float64Kind), false},
				types.Field{"f", types.MakePrimitiveType(types.StringKind), false},
			},
		),
	}, []ref.Ref{})
	__genPackageInFile_struct_with_unions_CachedRef = types.RegisterPackage(&p)
}
示例#5
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()
}
示例#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.MakeStructType("Tree",
			[]types.Field{
				types.Field{"children", types.MakeCompoundType(types.ListKind, types.MakeType(ref.Ref{}, 0)), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{})
	__genPackageInFile_struct_recursive_CachedRef = types.RegisterPackage(&p)
}
示例#7
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("StructWithDupList",
			[]types.Field{
				types.Field{"l", types.MakeCompoundType(types.ListKind, types.MakePrimitiveType(types.Uint8Kind)), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{})
	__genPackageInFile_struct_with_dup_list_CachedRef = types.RegisterPackage(&p)
}
示例#8
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("A",
			[]types.Field{
				types.Field{"A", types.MakeCompoundType(types.ListKind, types.MakeCompoundType(types.ListKind, types.MakePrimitiveType(types.BlobKind))), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{})
	__genPackageInFile_sha1_b3ecb0f_CachedRef = types.RegisterPackage(&p)
}
示例#9
0
文件: ref.noms.go 项目: arv/noms-old
// 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("StructWithRef",
			[]types.Field{
				types.Field{"r", types.MakeCompoundType(types.RefKind, types.MakeCompoundType(types.SetKind, types.MakePrimitiveType(types.Float32Kind))), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{})
	__genPackageInFile_ref_CachedRef = types.RegisterPackage(&p)
}
示例#10
0
文件: date.noms.go 项目: arv/noms-old
// 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("Date",
			[]types.Field{
				types.Field{"MsSinceEpoch", types.MakePrimitiveType(types.Int64Kind), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{})
	__commonPackageInFile_date_CachedRef = types.RegisterPackage(&p)
}
示例#11
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)
}
示例#12
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("Pitch",
			[]types.Field{
				types.Field{"X", types.MakePrimitiveType(types.Float64Kind), false},
				types.Field{"Z", types.MakePrimitiveType(types.Float64Kind), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{})
	__mainPackageInFile_types_CachedRef = types.RegisterPackage(&p)
}
示例#13
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("OptionalStruct",
			[]types.Field{
				types.Field{"s", types.MakePrimitiveType(types.StringKind), true},
				types.Field{"b", types.MakePrimitiveType(types.BoolKind), true},
			},
			types.Choices{},
		),
	}, []ref.Ref{})
	__genPackageInFile_struct_optional_CachedRef = types.RegisterPackage(&p)
}
示例#14
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("Commit",
			[]types.Field{
				types.Field{"value", types.MakePrimitiveType(types.ValueKind), false},
				types.Field{"parents", types.MakeCompoundType(types.SetKind, types.MakeCompoundType(types.RefKind, types.MakeType(ref.Ref{}, 0))), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{})
	__datasPackageInFile_types_CachedRef = types.RegisterPackage(&p)
}
示例#15
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)
}
示例#16
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)
}
示例#17
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("Song",
			[]types.Field{
				types.Field{"Title", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"Artist", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"Album", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"Year", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"Mp3", types.MakePrimitiveType(types.BlobKind), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{})
	__mainPackageInFile_types_CachedRef = types.RegisterPackage(&p)
}
示例#18
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("User",
			[]types.Field{
				types.Field{"Id", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"Name", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"Photos", types.MakeCompoundType(types.SetKind, types.MakeCompoundType(types.RefKind, types.MakeType(ref.Parse("sha1-10004087fdbc623873c649d28aa59f4e066d374e"), 0))), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{
		ref.Parse("sha1-10004087fdbc623873c649d28aa59f4e066d374e"),
	})
	__mainPackageInFile_facebook_CachedRef = types.RegisterPackage(&p)
}
示例#19
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("Import",
			[]types.Field{
				types.Field{"FileSHA1", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"Date", types.MakeType(ref.Parse("sha1-0b4ac7cb0583d7fecd71a1584a3f846e5d8b08eb"), 0), false},
				types.Field{"Companies", types.MakeCompoundType(types.RefKind, types.MakeCompoundType(types.MapKind, types.MakePrimitiveType(types.StringKind), types.MakeCompoundType(types.RefKind, types.MakeType(ref.Parse("sha1-91ae65b19b4817fc15d4e2c5c7472c68b4950b77"), 0)))), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{
		ref.Parse("sha1-0b4ac7cb0583d7fecd71a1584a3f846e5d8b08eb"),
		ref.Parse("sha1-91ae65b19b4817fc15d4e2c5c7472c68b4950b77"),
	})
	__mainPackageInFile_sha1_6c64b08_CachedRef = types.RegisterPackage(&p)
}
// 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("StructWithUnionField",
			[]types.Field{
				types.Field{"a", types.MakePrimitiveType(types.Float32Kind), false},
			},
			types.Choices{
				types.Field{"b", types.MakePrimitiveType(types.Float64Kind), false},
				types.Field{"c", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"d", types.MakePrimitiveType(types.BlobKind), false},
				types.Field{"e", types.MakePrimitiveType(types.ValueKind), false},
				types.Field{"f", types.MakeCompoundType(types.SetKind, types.MakePrimitiveType(types.Uint8Kind)), false},
			},
		),
	}, []ref.Ref{})
	__genPackageInFile_struct_with_union_field_CachedRef = types.RegisterPackage(&p)
}
示例#21
0
文件: geo.noms.go 项目: arv/noms-old
// 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("Geoposition",
			[]types.Field{
				types.Field{"Latitude", types.MakePrimitiveType(types.Float32Kind), false},
				types.Field{"Longitude", types.MakePrimitiveType(types.Float32Kind), false},
			},
			types.Choices{},
		),
		types.MakeStructType("Georectangle",
			[]types.Field{
				types.Field{"TopLeft", types.MakeType(ref.Ref{}, 0), false},
				types.Field{"BottomRight", types.MakeType(ref.Ref{}, 0), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{})
	__commonPackageInFile_geo_CachedRef = types.RegisterPackage(&p)
}
示例#22
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("D",
			[]types.Field{
				types.Field{"structField", types.MakeType(ref.Parse("sha1-1c216c6f1d6989e4ede5f78b7689214948dabeef"), 0), false},
				types.Field{"enumField", types.MakeType(ref.Parse("sha1-1c216c6f1d6989e4ede5f78b7689214948dabeef"), 1), false},
			},
			types.Choices{},
		),
		types.MakeStructType("DUser",
			[]types.Field{
				types.Field{"Dfield", types.MakeType(ref.Ref{}, 0), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{
		ref.Parse("sha1-1c216c6f1d6989e4ede5f78b7689214948dabeef"),
	})
	__genPackageInFile_sha1_eda4273_CachedRef = types.RegisterPackage(&p)
}
示例#23
0
文件: read.go 项目: arv/noms-old
// MakeStructTypeFromHeaders creates a struct type from the headers using |kinds| as the type of each field. If |kinds| is empty, default to strings.
func MakeStructTypeFromHeaders(headers []string, structName string, kinds KindSlice) (typeRef, typeDef types.Type) {
	useStringType := len(kinds) == 0
	d.Chk.True(useStringType || len(headers) == len(kinds))
	fields := make([]types.Field, len(headers))
	for i, key := range headers {
		kind := types.StringKind
		if !useStringType {
			kind = kinds[i]
		}
		fields[i] = types.Field{
			Name: key,
			T:    types.MakePrimitiveType(kind),
			// TODO(misha): Think about whether we need fields to be optional.
			Optional: false,
		}
	}
	typeDef = types.MakeStructType(structName, fields, types.Choices{})
	pkg := types.NewPackage([]types.Type{typeDef}, []ref.Ref{})
	pkgRef := types.RegisterPackage(&pkg)
	typeRef = types.MakeType(pkgRef, 0)

	return
}
示例#24
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))
}
示例#25
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("RemotePhoto",
			[]types.Field{
				types.Field{"Id", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"Title", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"Date", types.MakeType(ref.Parse("sha1-0b4ac7cb0583d7fecd71a1584a3f846e5d8b08eb"), 0), false},
				types.Field{"Geoposition", types.MakeType(ref.Parse("sha1-0cac0f1ed4777b6965548b0dfe6965a9f23af76c"), 0), false},
				types.Field{"Sizes", types.MakeCompoundType(types.MapKind, types.MakeType(ref.Ref{}, 2), types.MakePrimitiveType(types.StringKind)), false},
				types.Field{"Tags", types.MakeCompoundType(types.SetKind, types.MakePrimitiveType(types.StringKind)), false},
				types.Field{"Faces", types.MakeCompoundType(types.SetKind, types.MakeType(ref.Ref{}, 1)), false},
			},
			types.Choices{},
		),
		types.MakeStructType("Face",
			[]types.Field{
				types.Field{"Top", types.MakePrimitiveType(types.Float32Kind), false},
				types.Field{"Left", types.MakePrimitiveType(types.Float32Kind), false},
				types.Field{"Width", types.MakePrimitiveType(types.Float32Kind), false},
				types.Field{"Height", types.MakePrimitiveType(types.Float32Kind), false},
				types.Field{"PersonName", types.MakePrimitiveType(types.StringKind), false},
			},
			types.Choices{},
		),
		types.MakeStructType("Size",
			[]types.Field{
				types.Field{"Width", types.MakePrimitiveType(types.Uint32Kind), false},
				types.Field{"Height", types.MakePrimitiveType(types.Uint32Kind), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{
		ref.Parse("sha1-0b4ac7cb0583d7fecd71a1584a3f846e5d8b08eb"),
		ref.Parse("sha1-0cac0f1ed4777b6965548b0dfe6965a9f23af76c"),
	})
	__commonPackageInFile_photo_CachedRef = types.RegisterPackage(&p)
}
示例#26
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("StructPrimitives",
			[]types.Field{
				types.Field{"uint64", types.MakePrimitiveType(types.Uint64Kind), false},
				types.Field{"uint32", types.MakePrimitiveType(types.Uint32Kind), false},
				types.Field{"uint16", types.MakePrimitiveType(types.Uint16Kind), false},
				types.Field{"uint8", types.MakePrimitiveType(types.Uint8Kind), false},
				types.Field{"int64", types.MakePrimitiveType(types.Int64Kind), false},
				types.Field{"int32", types.MakePrimitiveType(types.Int32Kind), false},
				types.Field{"int16", types.MakePrimitiveType(types.Int16Kind), false},
				types.Field{"int8", types.MakePrimitiveType(types.Int8Kind), false},
				types.Field{"float64", types.MakePrimitiveType(types.Float64Kind), false},
				types.Field{"float32", types.MakePrimitiveType(types.Float32Kind), false},
				types.Field{"bool", types.MakePrimitiveType(types.BoolKind), false},
				types.Field{"string", types.MakePrimitiveType(types.StringKind), false},
				types.Field{"blob", types.MakePrimitiveType(types.BlobKind), false},
				types.Field{"value", types.MakePrimitiveType(types.ValueKind), false},
			},
			types.Choices{},
		),
	}, []ref.Ref{})
	__genPackageInFile_struct_primitives_CachedRef = types.RegisterPackage(&p)
}
示例#27
0
// FIXME: run with pipe
func (s *testSuite) TestCSVExporter() {
	storeName := "store"
	setName := "csv"
	header := []string{"a", "b", "c"}
	payload := [][]string{
		[]string{"5", "7", "100"},
		[]string{"4", "10", "255"},
		[]string{"512", "12", "55"},
	}
	structName := "SomeStruct"

	// Setup data store
	cs := chunks.NewLevelDBStore(s.LdbDir, storeName, 1, false)
	ds := dataset.NewDataset(datas.NewDataStore(cs), setName)

	// Build Struct fields based on header
	f := make([]types.Field, 0, len(header))
	for _, key := range header {
		f = append(f, types.Field{
			Name: key,
			T:    types.MakePrimitiveType(types.StringKind),
		})
	}

	typeDef := types.MakeStructType(structName, f, types.Choices{})
	pkg := types.NewPackage([]types.Type{typeDef}, []ref.Ref{})
	pkgRef := types.RegisterPackage(&pkg)
	typeRef := types.MakeType(pkgRef, 0)
	structFields := typeDef.Desc.(types.StructDesc).Fields

	// Build data rows
	structs := make([]types.Value, len(payload))
	for i, row := range payload {
		fields := make(map[string]types.Value)
		for j, v := range row {
			fields[structFields[j].Name] = types.NewString(v)
		}
		structs[i] = types.NewStruct(typeRef, typeDef, fields)
	}

	listType := types.MakeCompoundType(types.ListKind, typeRef)
	ds.Commit(types.NewTypedList(listType, structs...))
	ds.Store().Close()

	// Run exporter
	out := s.Run(main, []string{"-store", storeName, "-ds", setName})

	// Verify output
	csvReader := csv.NewReader(strings.NewReader(out))

	row, err := csvReader.Read()
	d.Chk.NoError(err)
	s.Equal(header, row)

	for i := 0; i < len(payload); i++ {
		row, err := csvReader.Read()
		d.Chk.NoError(err)
		s.Equal(payload[i], row)
	}

	row, err = csvReader.Read()
	s.Equal(io.EOF, err)
}