From 83fe4a4d3d793c7f54ed6903e58f82a506d2d10e Mon Sep 17 00:00:00 2001 From: connero <88785126+conneroisu@users.noreply.github.com> Date: Mon, 3 Jun 2024 13:04:40 -0500 Subject: [PATCH] latest --- parse.go | 8 ++-- parse_test.go | 85 +++++++++++--------------------------- structure.go => reflect.go | 0 specify.go | 1 - testdata/static.go | 3 -- tt.json | 1 - 6 files changed, 28 insertions(+), 70 deletions(-) rename structure.go => reflect.go (100%) delete mode 100644 specify.go delete mode 100644 tt.json diff --git a/parse.go b/parse.go index 5472488b4..20528dcb8 100644 --- a/parse.go +++ b/parse.go @@ -21,8 +21,8 @@ const ( // NewFromString parses a string into a slice of structs. // -// The struct must have a field with the tag `seltabl`, a header selector with -// the tag `hSel`, and a data selector with the tag `dSel`. +// The struct must have a field with the tag seltabl, a header selector with +// the tag hSel, and a data selector with the tag dSel. // // The selectors responsibilties: // @@ -92,7 +92,7 @@ func NewFromString[T any](htmlInput string) ([]T, error) { headSelector := field.Tag.Get(headerSelectorTag) if headSelector == "" { return nil, fmt.Errorf( - "no header selector (%s) for field %s", + "no header selector (%s) defined for field %s", headerSelectorTag, headName, ) @@ -100,7 +100,7 @@ func NewFromString[T any](htmlInput string) ([]T, error) { headerRow := doc.Find(headSelector) if headerRow.Length() == 0 { return nil, fmt.Errorf( - "no header for field %s with selector (%s)", + "selector (%s) found no header for field %s", headName, headSelector, ) diff --git a/parse_test.go b/parse_test.go index 25588ac21..123366c84 100644 --- a/parse_test.go +++ b/parse_test.go @@ -1,10 +1,8 @@ package seltabl import ( - "fmt" "testing" - "github.com/bytedance/sonic" "github.com/conneroisu/seltabl/testdata" "github.com/stretchr/testify/assert" ) @@ -13,9 +11,6 @@ import ( func TestFixtureTables(t *testing.T) { p, err := NewFromString[testdata.FixtureStruct](testdata.FixtureABNumTable) assert.Nil(t, err) - for _, pp := range p { - fmt.Printf("pp %+v\n", pp) - } assert.Equal(t, "1", p[0].A) assert.Equal(t, "2", p[0].B) assert.Equal(t, "3", p[1].A) @@ -31,7 +26,6 @@ func TestNumberedTable(t *testing.T) { p, err := NewFromString[testdata.NumberedStruct](testdata.NumberedTable) assert.Nil(t, err) assert.NoError(t, err) - assert.Equal(t, "Row 1, Cell 1", p[0].Header1) assert.Equal(t, "Row 1, Cell 2", p[0].Header2) assert.Equal(t, "Row 1, Cell 3", p[0].Header3) @@ -43,63 +37,32 @@ func TestNumberedTable(t *testing.T) { assert.Equal(t, "Row 3, Cell 3", p[2].Header3) } -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -//
SupernovaYearTypeDistance (light-years)Notes
SN 10061006Type Ia7,200Brightest recorded supernova in history
SN 1054 (Crab Nebula)1054Type II6,500Formed the Crab Nebula and pulsar
SN 1572 (Tycho's Supernova)1572Type Ia8,000-10,000Observed by Tycho Brahe
SN 1604 (Kepler's Supernova)1604Type Ia20,000Last observed supernova in the Milky Way
SN 1987A1987Type II168,000Closest observed supernova since 1604
SN 1993J1993Type IIb11,000,000In the galaxy M81
- +// TestSuperNovaTable tests the parsing of a table with supernova data. func TestSuperNovaTable(t *testing.T) { p, err := NewFromString[testdata.SuperNovaStruct](testdata.SuperNovaTable) assert.Nil(t, err) + assert.Equal(t, "SN 1006", p[0].Supernova) + assert.Equal(t, "1006", p[0].Year) + assert.Equal(t, "Type Ia", p[0].Type) + assert.Equal(t, "7,200", p[0].Distance) + assert.Equal(t, "Brightest recorded supernova in history", p[0].Notes) + + assert.Equal(t, "SN 1054 (Crab Nebula)", p[1].Supernova) + assert.Equal(t, "1054", p[1].Year) + assert.Equal(t, "Type II", p[1].Type) + assert.Equal(t, "6,500", p[1].Distance) + assert.Equal(t, "Formed the Crab Nebula and pulsar", p[1].Notes) + + assert.Equal(t, "SN 1572 (Tycho's Supernova)", p[2].Supernova) + assert.Equal(t, "1572", p[2].Year) + assert.Equal(t, "Type Ia", p[2].Type) + assert.Equal(t, "8,000-10,000", p[2].Distance) + assert.Equal(t, "Observed by Tycho Brahe", p[2].Notes) + + assert.Equal(t, "SN 1604 (Kepler's Supernova)", p[3].Supernova) + assert.Equal(t, "1604", p[3].Year) + assert.Equal(t, "Type Ia", p[3].Type) + assert.Equal(t, "20,000", p[3].Distance) + assert.Equal(t, "Last observed supernova in the Milky Way", p[3].Notes) - var data testdata.SuperNovaStruct - // Marshal - output, err := sonic.Marshal(&data) } diff --git a/structure.go b/reflect.go similarity index 100% rename from structure.go rename to reflect.go diff --git a/specify.go b/specify.go deleted file mode 100644 index 6add15c83..000000000 --- a/specify.go +++ /dev/null @@ -1 +0,0 @@ -package seltabl diff --git a/testdata/static.go b/testdata/static.go index 8f9df2099..530cacae9 100644 --- a/testdata/static.go +++ b/testdata/static.go @@ -31,6 +31,3 @@ type SuperNovaStruct struct { //go:embed supernova.html var SuperNovaTable string - -//go:embed supernova.json -var SuperNovaJSON string diff --git a/tt.json b/tt.json deleted file mode 100644 index 063397d07..000000000 --- a/tt.json +++ /dev/null @@ -1 +0,0 @@ -[{"Supernova":"SN 1006","Year":"1006","Type":"Type Ia","Distance":"7,200","Notes":"Brightest recorded supernova in history"},{"Supernova":"SN 1054 (Crab Nebula)","Year":"1054","Type":"Type II","Distance":"6,500","Notes":"Formed the Crab Nebula and pulsar"},{"Supernova":"SN 1572 (Tycho's Supernova)","Year":"1572","Type":"Type Ia","Distance":"8,000-10,000","Notes":"Observed by Tycho Brahe"},{"Supernova":"SN 1604 (Kepler's Supernova)","Year":"1604","Type":"Type Ia","Distance":"20,000","Notes":"Last observed supernova in the Milky Way"},{"Supernova":"SN 1987A","Year":"1987","Type":"Type II","Distance":"168,000","Notes":"Closest observed supernova since 1604"},{"Supernova":"SN 1993J","Year":"1993","Type":"Type IIb","Distance":"11,000,000","Notes":"In the galaxy M81"}]