diff --git a/datarecord/datarecord.go b/datarecord/datarecord.go index c422d72..90f91d2 100644 --- a/datarecord/datarecord.go +++ b/datarecord/datarecord.go @@ -5,11 +5,10 @@ import ( "bytes" "fmt" "log" + "sort" "strconv" "strings" "time" - - "golang.org/x/exp/maps" ) type void struct{} @@ -82,15 +81,12 @@ func (obj *dataReader) ReadDataRecord(data string) { func (obj *dataReader) GetColumns() []string { columns := make([]string, 0, 10) - if len(obj.columnNames) == 0 { - for i := 1; i < obj.points+1; i++ { - columns = append(columns, fmt.Sprintf("Column %d", i)) + for _, name := range obj.getColumnNames() { + if name == "" { + name = "Column" } - } else { - for name := range obj.columnNames { - for i := 1; i < obj.points+1; i++ { - columns = append(columns, fmt.Sprintf("%s%d", name, i)) - } + for i := 1; i < obj.points+1; i++ { + columns = append(columns, fmt.Sprintf("%s %d", name, i)) } } @@ -101,16 +97,13 @@ func (obj *dataReader) GetDataRows() []string { rows := make([]string, 0, 10) - columns := maps.Keys(obj.columnNames) - if len(columns) == 0 { - columns = append(columns, "") - } + columns := obj.getColumnNames() buffer := new(bytes.Buffer) writer := bufio.NewWriter(buffer) for i := 0; i < obj.points; i++ { - writer.WriteString(", ") + writer.WriteString(", null") } writer.Flush() blankPoints := buffer.String() @@ -143,6 +136,23 @@ func (obj *dataReader) GetDataRows() []string { return rows } +/////////////////////////////////////////////////////// + +func (obj *dataReader) getColumnNames() []string { + columns := make([]string, 0, 10) + if len(obj.columnNames) == 0 { + columns = append(columns, "") + } else { + for name := range obj.columnNames { + columns = append(columns, name) + } + } + + sort.Strings(columns) + + return columns +} + /////////////////////////////////////////////////////// // dateRecord @@ -176,24 +186,3 @@ func (obj *dataReader) getDataRecord(data string) (record dataRecord) { return } - -// func (obj *dataRecord) String() string { -// buffer := new(bytes.Buffer) -// writer := bufio.NewWriter(buffer) - -// //[new Date(2314, 2, 16), {"": [24045, 12374]}], - -// writer.WriteString("[") -// writer.WriteString(fmt.Sprintf("new Date(%s)", obj.dateTime.Format("2006, 01, 02, 15, 04, 05"))) -// for _, point := range obj.points { -// writer.WriteString(fmt.Sprintf(", %g", point)) -// } -// writer.WriteString("]") - -// writer.Flush() -// return buffer.String() -// } - -func (obj *dataRecord) Columns() int { - return len(obj.points) -} diff --git a/datarecord/datarecord_test.go b/datarecord/datarecord_test.go index ad583b9..958ec34 100644 --- a/datarecord/datarecord_test.go +++ b/datarecord/datarecord_test.go @@ -94,11 +94,11 @@ func Test_dataReader_GetColumns(t *testing.T) { }{ { "test 1", dataReader{columnNames: map[string]void{"first": {}, "second": {}}, points: 3}, - []string{"first1", "first2", "first3", "second1", "second2", "second3"}, + []string{"first 1", "first 2", "first 3", "second 1", "second 2", "second 3"}, }, { "test 2", dataReader{columnNames: map[string]void{}, points: 3}, - []string{"Column1", "Column2", "Column3"}, + []string{"Column 1", "Column 2", "Column 3"}, }, } for _, tt := range tests { diff --git a/toGraph_test.go b/toGraph_test.go deleted file mode 100644 index feeda1d..0000000 --- a/toGraph_test.go +++ /dev/null @@ -1,175 +0,0 @@ -package main - -import ( - "bytes" - "strings" - "testing" - - state "github.com/nikolainp/toGraph/statecontext" -) - -func Test_processSimpleFile(t *testing.T) { - var config state.Configuration - config.DateFormat = "20060102150405" - config.DateColumn = 1 - - tests := []struct { - name string - sInput string - wantSOut string - wantErr bool - }{ - {"Test 1", - `20121015100000 1 2 3 -20121015100030 2 3 4 -20121015100100 1 2 3 -20121015100130 2 3 4 -20121015100200 1 2 3`, - ` - - - - My page - - - - - - -
- - -`, - false, - }, - } - - replacer := strings.NewReplacer(" ", "", "\n", "", "\t", "") - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - sIn := strings.NewReader(tt.sInput) - sOut := &bytes.Buffer{} - if err := processFile(sIn, sOut, config); (err != nil) != tt.wantErr { - t.Errorf("run() error = %v, wantErr %v", err, tt.wantErr) - return - } - if gotSOut := sOut.String(); replacer.Replace(gotSOut) != replacer.Replace(tt.wantSOut) { - t.Errorf("run() = %v, want %v", gotSOut, tt.wantSOut) - } - }) - } -} - -func Test_processPivotFile(t *testing.T) { - var config state.Configuration - config.DateFormat = "20060102150405" - config.DateColumn = 1 - config.PivotColumn = 2 - - tests := []struct { - name string - sInput string - wantSOut string - wantErr bool - }{ - {"Test 1", - `20121015100000 first 1 2 3 -20121015100000 second 2 3 4 -20121015100100 first 1 2 3 -20121015100100 second 2 3 4 -20121015100200 first 1 2 3`, - ` - - - - My page - - - - - - -
- - -`, - false, - }, - } - - replacer := strings.NewReplacer(" ", "", "\n", "", "\t", "") - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - sIn := strings.NewReader(tt.sInput) - sOut := &bytes.Buffer{} - if err := processFile(sIn, sOut, config); (err != nil) != tt.wantErr { - t.Errorf("run() error = %v, wantErr %v", err, tt.wantErr) - return - } - if gotSOut := sOut.String(); replacer.Replace(gotSOut) != replacer.Replace(tt.wantSOut) { - t.Errorf("run() = %v, want %v", gotSOut, tt.wantSOut) - } - }) - } -}