Skip to content

Commit

Permalink
Fix and test subtle table cell literal parsing bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
urban-wombat committed Apr 15, 2020
1 parent a877aeb commit fcade22
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
4 changes: 2 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ var floatRegexp *regexp.Regexp = regexp.MustCompile(`^([-+]?([0-9]+(\.[0-9]*)?|\

const tableSetNamePattern string = `^\[\[[a-zA-Z_][a-zA-Z0-9_]*\]\]$`
const namePattern string = `^[a-zA-Z_][a-zA-Z0-9_]*$`
const tableNamePattern string = `^\[[a-zA-Z_][a-zA-Z0-9_]*\]$`
const tableNameNilPattern string = `^(\[\])`
const tableNamePattern string = `^\[[a-zA-Z_][a-zA-Z0-9_]*\]` // Don't add $ at end of this regular expression.
const tableNameNilPattern string = `^(\[\])` // Don't add $ at end of this regular expression.

// From: https://golang.org/pkg/time
// RFC3339 = "2006-01-02T15:04:05Z07:00"
Expand Down
59 changes: 59 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,62 @@ func ExampleNewNilTable_createAndUse() {
// [NilTableNoLonger]
// my_col string = "my_string_value"
}

func ExampleTable_GetTable_cellTableInTabular() {
// A table literal. Sometimes easier than constructing a table programmatically.
tableString := `[MyTable]
MyBool MyString MyInt MyTable MyTable2
bool string int *Table *gotables.Table
true "The answer to life, the universe and everything." 42 [CellTable] [CellTable2]
`
// Note 1: The only string form of a table cell containing a *Table is its table name in square brackets.
// Note 2: To get a table cell *Table as a string, first retrieve it to a variable.
// Note 3: It is parsed into an empty table with the name specified.

table, err := NewTableFromString(tableString)
if err != nil {
log.Println(err)
}

table.SetStructShape(false)

fmt.Println(table)

myTable, err := table.GetTable("MyTable", 0)
if err != nil {
log.Println(err)
}

err = myTable.AppendRow()
if err != nil {
log.Println(err)
}

err = myTable.AppendCol("msg", "string")
if err != nil {
log.Println(err)
}

err = myTable.SetString("msg", 0, "I am in a table in a cell!")
if err != nil {
log.Println(err)
}

err = myTable.SetStructShape(true)
if err != nil {
log.Println(err)
}

fmt.Println(myTable)

// Note: The struct/tabular shape is for readability and has no impact on its internal structure.

// Output:
// [MyTable]
// MyBool MyString MyInt MyTable MyTable2
// bool string int *Table *gotables.Table
// true "The answer to life, the universe and everything." 42 [CellTable] [CellTable2]
//
// [CellTable]
// msg string = "I am in a table in a cell!"
}

0 comments on commit fcade22

Please sign in to comment.