浏览代码

Changed from pointers to values

DamianSzkuat 6 年之前
父节点
当前提交
b2de99d7e3
共有 4 个文件被更改,包括 66 次插入90 次删除
  1. 9 17
      stream_file.go
  2. 6 20
      stream_file_builder.go
  3. 19 21
      stream_style.go
  4. 32 32
      stream_test.go

+ 9 - 17
stream_file.go

@@ -16,7 +16,7 @@ type StreamFile struct {
 	currentSheet   *streamSheet
 	styleIds       [][]int
 	err            error
-	styleIdMap	   map[*StreamStyle]int
+	styleIdMap	   map[StreamStyle]int
 }
 
 type streamSheet struct {
@@ -44,7 +44,7 @@ var (
 // same number of cells as the header provided when the sheet was created or an error will be returned. This function
 // will always trigger a flush on success. Currently the only supported data type is string data.
 // TODO update comment
-func (sf *StreamFile) Write(cells []string, cellTypes []*CellType, cellStyles []*StreamStyle) error {
+func (sf *StreamFile) Write(cells []string, cellTypes []CellType, cellStyles []StreamStyle) error {
 	if sf.err != nil {
 		return sf.err
 	}
@@ -57,7 +57,7 @@ func (sf *StreamFile) Write(cells []string, cellTypes []*CellType, cellStyles []
 }
 
 //TODO Add comment
-func (sf *StreamFile) WriteAll(records [][]string, cellTypes []*CellType, cellStyles []*StreamStyle) error {
+func (sf *StreamFile) WriteAll(records [][]string, cellTypes []CellType, cellStyles []StreamStyle) error {
 	if sf.err != nil {
 		return sf.err
 	}
@@ -72,7 +72,7 @@ func (sf *StreamFile) WriteAll(records [][]string, cellTypes []*CellType, cellSt
 }
 
 // TODO Add comment
-func (sf *StreamFile) write(cells []string, cellTypes []*CellType, cellStyles []*StreamStyle) error {
+func (sf *StreamFile) write(cells []string, cellTypes []CellType, cellStyles []StreamStyle) error {
 	if sf.currentSheet == nil {
 		return NoCurrentSheetError
 	}
@@ -148,7 +148,7 @@ func (sf *StreamFile) write(cells []string, cellTypes []*CellType, cellStyles []
 }
 
 
-func GetCellTypeAsString(cellType *CellType) (string, error) {
+func GetCellTypeAsString(cellType CellType) (string, error) {
 	// documentation for the c.t (cell.Type) attribute:
 	// b (Boolean): Cell containing a boolean.
 	// d (Date): Cell contains a date in the ISO 8601 format.
@@ -158,11 +158,7 @@ func GetCellTypeAsString(cellType *CellType) (string, error) {
 	// n (Number): Cell containing a number.
 	// s (Shared String): Cell containing a shared string.
 	// str (String): Cell containing a formula string.
-	if cellType == nil {
-		// TODO should default be inline string?
-		return "inlineStr", nil
-	}
-	switch *cellType{
+	switch cellType{
 	case CellTypeBool:
 		return "b", nil
 	case CellTypeDate:
@@ -185,17 +181,13 @@ func GetCellTypeAsString(cellType *CellType) (string, error) {
 	}
 }
 
-func GetCellContentOpenAncCloseTags(cellType *CellType) (string, string, error) {
-	if cellType == nil {
-		// TODO should default be inline string?
-		return `<is><t>`, `</t></is>`, nil
-	}
+func GetCellContentOpenAncCloseTags(cellType CellType) (string, string, error) {
 	// TODO Currently inline strings are types as shared strings
 	// TODO remove once the tests have been changed
-	if *cellType == CellTypeString {
+	if cellType == CellTypeString {
 		return `<is><t>`, `</t></is>`, nil
 	}
-	switch *cellType{
+	switch cellType{
 	case CellTypeInline:
 		return `<is><t>`, `</t></is>`, nil
 	case CellTypeStringFormula:

+ 6 - 20
stream_file_builder.go

@@ -39,7 +39,7 @@ type StreamFileBuilder struct {
 	// cellTypeToStyleIds map[CellType]int
 	maxStyleId         int
 	styleIds           [][]int
-	styleIdMap		   map[*StreamStyle]int
+	styleIdMap		   map[StreamStyle]int
 }
 
 const (
@@ -62,7 +62,7 @@ func NewStreamFileBuilder(writer io.Writer) *StreamFileBuilder {
 		xlsxFile:           NewFile(),
 		// cellTypeToStyleIds: make(map[CellType]int),
 		maxStyleId:         initMaxStyleId,
-		styleIdMap:			make(map[*StreamStyle]int),
+		styleIdMap:			make(map[StreamStyle]int),
 	}
 }
 
@@ -79,7 +79,7 @@ func NewStreamFileBuilderForPath(path string) (*StreamFileBuilder, error) {
 // AddSheet will add sheets with the given name with the provided headers. The headers cannot be edited later, and all
 // rows written to the sheet must contain the same number of cells as the header. Sheet names must be unique, or an
 // error will be thrown.
-func (sb *StreamFileBuilder) AddSheet(name string, headers []string, cellStyles []*StreamStyle, cellTypes []*CellType) error {
+func (sb *StreamFileBuilder) AddSheet(name string, headers []string, cellStyles []StreamStyle, cellTypes []CellType) error {
 	if sb.built {
 		return BuiltStreamFileBuilderError
 	}
@@ -101,21 +101,7 @@ func (sb *StreamFileBuilder) AddSheet(name string, headers []string, cellStyles
 	}
 	for i, cellType := range cellTypes {
 		cellStyleIndex := sb.styleIdMap[cellStyles[i]]
-		//var ok bool
-		if cellType != nil {
-			// The cell type is one of the attributes of a Style.
-			// Since it is the only attribute of Style that we use, we can assume that cell types
-			// map one to one with Styles and their Style ID.
-			// If a new cell type is used, a new style gets created with an increased id, if an existing cell type is
-			// used, the pre-existing style will also be used.
-			//cellStyleIndex, ok = sb.cellTypeToStyleIds[*cellType]
-			//if !ok {
-			//	sb.maxStyleId++
-			//	cellStyleIndex = sb.maxStyleId
-			//	sb.cellTypeToStyleIds[*cellType] = sb.maxStyleId
-			//}
-			sheet.Cols[i].SetType(*cellType)
-		}
+		sheet.Cols[i].SetType(cellType)
 		sb.styleIds[len(sb.styleIds)-1] = append(sb.styleIds[len(sb.styleIds)-1], cellStyleIndex)
 	}
 	return nil
@@ -175,10 +161,10 @@ func (sb *StreamFileBuilder) addDefaultStyles(parts map[string]string) (map[stri
 	var err error
 
 	for _,streamStyle := range DefaultStyles{
-		if streamStyle != nil{
+		//if streamStyle != nil{
 			XfId := handleStyleForXLSX(streamStyle.style, streamStyle.xNumFmtId, sb.xlsxFile.styles)
 			sb.styleIdMap[streamStyle] = XfId
-		}
+		//}
 	}
 
 	parts["xl/styles.xml"], err = sb.xlsxFile.styles.Marshal()

+ 19 - 21
stream_style.go

@@ -7,35 +7,35 @@ type StreamStyle struct {
 	style 		*Style
 }
 
-var DefaultStringStyle *StreamStyle
-var DefaultStringBoldStyle *StreamStyle
-var DefaultStringItalicStyle *StreamStyle
-var DefaultStringUnderlinedStyle *StreamStyle
+var DefaultStringStyle StreamStyle
+var DefaultStringBoldStyle StreamStyle
+var DefaultStringItalicStyle StreamStyle
+var DefaultStringUnderlinedStyle StreamStyle
 
-var DefaultNumericStyle *StreamStyle
-var DefaultNumericBoldStyle *StreamStyle
-var DefaultNumericItalicStyle *StreamStyle
-var DefaultNumericUnderlinedStyle *StreamStyle
+var DefaultNumericStyle StreamStyle
+var DefaultNumericBoldStyle StreamStyle
+var DefaultNumericItalicStyle StreamStyle
+var DefaultNumericUnderlinedStyle StreamStyle
 
-var DefaultStyles []*StreamStyle
+var DefaultStyles []StreamStyle
 
 func init(){
 	// default string styles
-	DefaultStringStyle = &StreamStyle{
+	DefaultStringStyle = StreamStyle{
 		xNumFmtId: 0,
 		style: NewStyle(),
 	}
-	DefaultStringBoldStyle = &StreamStyle{
+	DefaultStringBoldStyle = StreamStyle{
 		xNumFmtId: 0,
 		style: NewStyle(),
 	}
 	DefaultStringBoldStyle.style.Font.Bold = true
-	DefaultStringItalicStyle = &StreamStyle{
+	DefaultStringItalicStyle = StreamStyle{
 		xNumFmtId: 0,
 		style: NewStyle(),
 	}
 	DefaultStringItalicStyle.style.Font.Italic = true
-	DefaultStringUnderlinedStyle = &StreamStyle{
+	DefaultStringUnderlinedStyle = StreamStyle{
 		xNumFmtId: 0,
 		style: NewStyle(),
 	}
@@ -47,21 +47,21 @@ func init(){
 	DefaultStyles = append(DefaultStyles, DefaultStringUnderlinedStyle)
 
 	// default string styles
-	DefaultNumericStyle = &StreamStyle{
+	DefaultNumericStyle = StreamStyle{
 		xNumFmtId: 1,
 		style: NewStyle(),
 	}
-	DefaultNumericBoldStyle = &StreamStyle{
+	DefaultNumericBoldStyle = StreamStyle{
 		xNumFmtId: 1,
 		style: NewStyle(),
 	}
 	DefaultNumericBoldStyle.style.Font.Bold = true
-	DefaultNumericItalicStyle = &StreamStyle{
+	DefaultNumericItalicStyle = StreamStyle{
 		xNumFmtId: 1,
 		style: NewStyle(),
 	}
 	DefaultNumericItalicStyle.style.Font.Italic = true
-	DefaultNumericUnderlinedStyle = &StreamStyle{
+	DefaultNumericUnderlinedStyle = StreamStyle{
 		xNumFmtId: 1,
 		style: NewStyle(),
 	}
@@ -71,13 +71,11 @@ func init(){
 	DefaultStyles = append(DefaultStyles, DefaultNumericBoldStyle)
 	DefaultStyles = append(DefaultStyles, DefaultNumericItalicStyle)
 	DefaultStyles = append(DefaultStyles, DefaultNumericUnderlinedStyle)
-
-
 }
 
 // MakeStyle creates a new StreamStyle and add it to the styles that will be streamed
 // This function returns a reference to the created StreamStyle
-func MakeStyle(formatStyleId int, font Font, fill Fill, alignment Alignment, border Border) *StreamStyle {
+func MakeStyle(formatStyleId int, font Font, fill Fill, alignment Alignment, border Border) StreamStyle {
 	newStyle := NewStyle()
 
 	newStyle.Font = font
@@ -90,7 +88,7 @@ func MakeStyle(formatStyleId int, font Font, fill Fill, alignment Alignment, bor
 	newStyle.ApplyAlignment = true
 	newStyle.ApplyBorder = true
 
-	newStreamStyle := &StreamStyle{
+	newStreamStyle := StreamStyle{
 		xNumFmtId: 	formatStyleId,
 		style: 		newStyle,
 	}

+ 32 - 32
stream_test.go

@@ -11,7 +11,7 @@ import (
 )
 
 const (
-	TestsShouldMakeRealFiles = false
+	TestsShouldMakeRealFiles = true
 )
 
 type StreamSuite struct{}
@@ -33,8 +33,8 @@ func (s *StreamSuite) TestXlsxStreamWrite(t *C) {
 		testName      string
 		sheetNames    []string
 		workbookData  [][][]string
-		cellStyles    [][][]*StreamStyle
-		cellTypes     [][][]*CellType
+		cellStyles    [][][]StreamStyle
+		cellTypes     [][][]CellType
 		expectedError error
 	}{
 		{
@@ -48,16 +48,16 @@ func (s *StreamSuite) TestXlsxStreamWrite(t *C) {
 					{"1234", "98", "34", "26032019"},
 				},
 			},
-			cellStyles: [][][]*StreamStyle{
+			cellStyles: [][][]StreamStyle{
 				{
 					{DefaultStringStyle,  DefaultStringBoldStyle,  DefaultStringItalicStyle,  DefaultStringUnderlinedStyle},
 					{DefaultNumericStyle, DefaultNumericBoldStyle, DefaultNumericItalicStyle, DefaultNumericUnderlinedStyle},
 				},
 			},
-			cellTypes: [][][]*CellType{
+			cellTypes: [][][]CellType{
 				{
-					{CellTypeString.Ptr(),  CellTypeString.Ptr(),  CellTypeString.Ptr(),  CellTypeString.Ptr()},
-					{CellTypeNumeric.Ptr(), CellTypeNumeric.Ptr(), CellTypeNumeric.Ptr(), CellTypeNumeric.Ptr()},
+					{CellTypeString,  CellTypeString,  CellTypeString,  CellTypeString},
+					{CellTypeNumeric, CellTypeNumeric, CellTypeNumeric, CellTypeNumeric},
 				},
 			},
 		},
@@ -72,10 +72,10 @@ func (s *StreamSuite) TestXlsxStreamWrite(t *C) {
 					{"123", "Taco", "300", "0000000123"},
 				},
 			},
-			cellTypes: [][][]*CellType{
+			cellTypes: [][][]CellType{
 				{
-					{CellTypeString.Ptr(),  CellTypeString.Ptr(),  CellTypeString.Ptr(),  CellTypeString.Ptr()},
-					{CellTypeNumeric.Ptr(), CellTypeString.Ptr(),  nil,                   CellTypeString.Ptr()},
+					{CellTypeString,  CellTypeString,  CellTypeString,  CellTypeString},
+					{CellTypeNumeric, CellTypeString,  CellTypeNumeric, CellTypeString},
 				},
 			},
 		},
@@ -269,11 +269,11 @@ func (s *StreamSuite) TestXlsxStreamWrite(t *C) {
 		}
 
 		if testCase.cellStyles == nil {
-			testCase.cellStyles = [][][]*StreamStyle{}
+			testCase.cellStyles = [][][]StreamStyle{}
 			for j,_ := range testCase.workbookData{
-				testCase.cellStyles = append(testCase.cellStyles, [][]*StreamStyle{})
+				testCase.cellStyles = append(testCase.cellStyles, [][]StreamStyle{})
 				for k,_ := range testCase.workbookData[j]{
-					testCase.cellStyles[j] = append(testCase.cellStyles[j], []*StreamStyle{})
+					testCase.cellStyles[j] = append(testCase.cellStyles[j], []StreamStyle{})
 					for  _,_ = range testCase.workbookData[j][k]{
 						testCase.cellStyles[j][k] = append(testCase.cellStyles[j][k], DefaultStringStyle)
 					}
@@ -282,14 +282,14 @@ func (s *StreamSuite) TestXlsxStreamWrite(t *C) {
 		}
 
 		if testCase.cellTypes == nil {
-			testCase.cellTypes = [][][]*CellType{}
+			testCase.cellTypes = [][][]CellType{}
 			//testCase.cellTypes = append(testCase.cellTypes, [][]*CellType{})
 			for j,_ := range testCase.workbookData{
-				testCase.cellTypes = append(testCase.cellTypes, [][]*CellType{})
+				testCase.cellTypes = append(testCase.cellTypes, [][]CellType{})
 				for k,_ := range testCase.workbookData[j]{
-					testCase.cellTypes[j] = append(testCase.cellTypes[j], []*CellType{})
+					testCase.cellTypes[j] = append(testCase.cellTypes[j], []CellType{})
 					for  _,_ = range testCase.workbookData[j][k]{
-						testCase.cellTypes[j][k] = append(testCase.cellTypes[j][k], CellTypeString.Ptr())
+						testCase.cellTypes[j][k] = append(testCase.cellTypes[j][k], CellTypeString)
 					}
 				}
 			}
@@ -373,7 +373,7 @@ func (s *StreamSuite) TestXlsxStyleBehavior(t *C) {
 }
 
 // writeStreamFile will write the file using this stream package
-func writeStreamFile(filePath string, fileBuffer io.Writer, sheetNames []string, workbookData [][][]string, cellStyles [][][]*StreamStyle, cellTypes [][][]*CellType, shouldMakeRealFiles bool) error {
+func writeStreamFile(filePath string, fileBuffer io.Writer, sheetNames []string, workbookData [][][]string, cellStyles [][][]StreamStyle, cellTypes [][][]CellType, shouldMakeRealFiles bool) error {
 	var file *StreamFileBuilder
 	var err error
 	if shouldMakeRealFiles {
@@ -388,7 +388,7 @@ func writeStreamFile(filePath string, fileBuffer io.Writer, sheetNames []string,
 	for i, sheetName := range sheetNames {
 		header := workbookData[i][0]
 		headerCellStyles := cellStyles[i][0]
-		var sheetHeaderTypes []*CellType
+		var sheetHeaderTypes []CellType
 		if i < len(cellTypes) {
 			sheetHeaderTypes = cellTypes[i][0]
 		}
@@ -469,11 +469,11 @@ func readXLSXFile(t *C, filePath string, fileBuffer io.ReaderAt, size int64, sho
 func (s *StreamSuite) TestAddSheetErrorsAfterBuild(t *C) {
 	file := NewStreamFileBuilder(bytes.NewBuffer(nil))
 
-	err := file.AddSheet("Sheet1", []string{"Header"}, []*StreamStyle{DefaultStringStyle}, nil)
+	err := file.AddSheet("Sheet1", []string{"Header"}, []StreamStyle{DefaultStringStyle}, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
-	err = file.AddSheet("Sheet2", []string{"Header2"}, []*StreamStyle{DefaultStringStyle}, nil)
+	err = file.AddSheet("Sheet2", []string{"Header2"}, []StreamStyle{DefaultStringStyle}, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -482,7 +482,7 @@ func (s *StreamSuite) TestAddSheetErrorsAfterBuild(t *C) {
 	if err != nil {
 		t.Fatal(err)
 	}
-	err = file.AddSheet("Sheet3", []string{"Header3"}, []*StreamStyle{DefaultStringStyle}, nil)
+	err = file.AddSheet("Sheet3", []string{"Header3"}, []StreamStyle{DefaultStringStyle}, nil)
 	if err != BuiltStreamFileBuilderError {
 		t.Fatal(err)
 	}
@@ -491,11 +491,11 @@ func (s *StreamSuite) TestAddSheetErrorsAfterBuild(t *C) {
 func (s *StreamSuite) TestBuildErrorsAfterBuild(t *C) {
 	file := NewStreamFileBuilder(bytes.NewBuffer(nil))
 
-	err := file.AddSheet("Sheet1", []string{"Header"}, []*StreamStyle{DefaultStringStyle}, nil)
+	err := file.AddSheet("Sheet1", []string{"Header"}, []StreamStyle{DefaultStringStyle}, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
-	err = file.AddSheet("Sheet2", []string{"Header2"}, []*StreamStyle{DefaultStringStyle}, nil)
+	err = file.AddSheet("Sheet2", []string{"Header2"}, []StreamStyle{DefaultStringStyle}, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -519,13 +519,13 @@ func (s *StreamSuite) TestCloseWithNothingWrittenToSheets(t *C) {
 		{{"Header1", "Header2"}},
 		{{"Header3", "Header4"}},
 	}
-	cellStyles := [][][]*StreamStyle{
+	cellStyles := [][][]StreamStyle{
 		{{DefaultStringStyle, DefaultStringStyle}},
 		{{DefaultStringStyle, DefaultStringStyle}},
 	}
-	cellTypes := [][][]*CellType{
-		{{CellTypeString.Ptr(), CellTypeString.Ptr()}},
-		{{CellTypeString.Ptr(), CellTypeString.Ptr()}},
+	cellTypes := [][][]CellType{
+		{{CellTypeString, CellTypeString}},
+		{{CellTypeString, CellTypeString}},
 	}
 	err := file.AddSheet(sheetNames[0], workbookData[0][0], cellStyles[0][0], cellTypes[0][0])
 	if err != nil {
@@ -582,16 +582,16 @@ func (s *StreamSuite) TestMakeNewStyleAndUseIt(t *C){
 			{"Good", "Bad"},
 		},
 	}
-	cellStyles := [][][]*StreamStyle{
+	cellStyles := [][][]StreamStyle{
 		{
 			{DefaultStringStyle, DefaultStringStyle},
 			{greenStyle,         redStyle},
 		},
 	}
-	cellTypes := [][][]*CellType{
+	cellTypes := [][][]CellType{
 		{
-			{CellTypeString.Ptr(), CellTypeString.Ptr()},
-			{CellTypeString.Ptr(), CellTypeString.Ptr()},
+			{CellTypeString, CellTypeString},
+			{CellTypeString, CellTypeString},
 		},
 	}