-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement Parse func and refactor DataSourcer interface (#4)
* feat: Add Item properties * refactor: DataSourcer returns Source interface * feat: Extend Source interface with CreatedAt and ModifiedAt and FileSource implementation * feat: Implement Parser
- Loading branch information
1 parent
b331e19
commit 14a3b9f
Showing
12 changed files
with
260 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
module github.com/glass-cms/glasscms | ||
|
||
go 1.22 | ||
go 1.22.0 | ||
|
||
require ( | ||
github.com/djherbis/times v1.6.0 | ||
github.com/spf13/cobra v1.8.0 | ||
github.com/stretchr/testify v1.9.0 | ||
gopkg.in/yaml.v3 v3.0.1 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
package item | ||
|
||
type Item struct{} | ||
import "time" | ||
|
||
type Item struct { | ||
Title string | ||
Content string | ||
CreateTime time.Time | ||
UpdateTime time.Time | ||
Properties map[string]any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,48 @@ | ||
package parser | ||
|
||
import "github.com/glass-cms/glasscms/item" | ||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
|
||
type Parser struct { | ||
Config Config | ||
} | ||
"github.com/glass-cms/glasscms/item" | ||
"github.com/glass-cms/glasscms/sourcer" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
const ( | ||
numParts = 3 | ||
) | ||
|
||
func NewParser(config Config) *Parser { | ||
return &Parser{ | ||
Config: config, | ||
// Parse reads the content of a source and returns an item. | ||
func Parse(src sourcer.Source) (*item.Item, error) { | ||
c, err := io.ReadAll(src) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
defer src.Close() | ||
|
||
type Config struct { | ||
} | ||
// Split the content into front matter and markdown | ||
parts := bytes.SplitN(c, []byte("---\n"), numParts) | ||
if len(parts) < numParts { | ||
return nil, errors.New("invalid content") | ||
} | ||
|
||
// Parse the YAML front matter | ||
var properties map[string]any | ||
err = yaml.Unmarshal(parts[1], &properties) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Keep the markdown content as is | ||
content := string(parts[2]) | ||
|
||
func (p *Parser) Parse(_ string) (*item.Item, error) { | ||
return &item.Item{}, nil | ||
return &item.Item{ | ||
Title: src.Name(), | ||
Content: content, | ||
CreateTime: src.CreatedAt(), | ||
UpdateTime: src.ModifiedAt(), | ||
Properties: properties, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package parser_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"strings" | ||
|
||
"github.com/glass-cms/glasscms/parser" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type MockSource struct { | ||
name string | ||
reader *strings.Reader | ||
} | ||
|
||
func NewMockSource(name string, data string) *MockSource { | ||
return &MockSource{ | ||
name: name, | ||
reader: strings.NewReader(data), | ||
} | ||
} | ||
|
||
func (m *MockSource) Read(p []byte) (int, error) { | ||
return m.reader.Read(p) | ||
} | ||
|
||
func (m *MockSource) Close() error { | ||
return nil | ||
} | ||
|
||
func (m *MockSource) Name() string { | ||
return m.name | ||
} | ||
|
||
func (m *MockSource) CreatedAt() time.Time { | ||
return time.Now() | ||
} | ||
|
||
func (m *MockSource) ModifiedAt() time.Time { | ||
return time.Now() | ||
} | ||
|
||
func TestParse(t *testing.T) { | ||
// Arrange | ||
source := NewMockSource("test", "---\ntitle: Test\n---\n# Test\n") | ||
|
||
// Act | ||
item, err := parser.Parse(source) | ||
|
||
// Assert | ||
require.NoError(t, err) | ||
assert.NotNil(t, item) | ||
assert.Equal(t, "test", item.Title) | ||
assert.Equal(t, "# Test\n", item.Content) | ||
assert.Equal(t, map[string]interface{}{"title": "Test"}, item.Properties) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package sourcer | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/djherbis/times" | ||
) | ||
|
||
var _ Source = (*FileSource)(nil) | ||
|
||
type FileSource struct { | ||
*os.File | ||
birthtime time.Time | ||
modtime time.Time | ||
} | ||
|
||
func NewFileSource(file *os.File) (*FileSource, error) { | ||
stats, err := times.StatFile(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &FileSource{ | ||
File: file, | ||
birthtime: stats.BirthTime(), | ||
modtime: stats.ModTime(), | ||
}, nil | ||
} | ||
|
||
func (f FileSource) CreatedAt() time.Time { | ||
return f.birthtime | ||
} | ||
|
||
func (f FileSource) ModifiedAt() time.Time { | ||
return f.modtime | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package sourcer_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/djherbis/times" | ||
"github.com/glass-cms/glasscms/sourcer" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func createTempFile() (*os.File, error) { | ||
tempFile, err := os.CreateTemp("", "source") | ||
if err != nil { | ||
return nil, err | ||
} | ||
return tempFile, nil | ||
} | ||
|
||
func TestNewFileSource(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Arrange | ||
tempFile, err := createTempFile() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(tempFile.Name()) | ||
|
||
// Act | ||
fileSource, err := sourcer.NewFileSource(tempFile) | ||
|
||
// Assert | ||
require.NoError(t, err) | ||
assert.NotNil(t, fileSource) | ||
|
||
stats, err := times.StatFile(tempFile) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.Equal(t, stats.BirthTime(), fileSource.CreatedAt()) | ||
assert.Equal(t, stats.ModTime(), fileSource.ModifiedAt()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package sourcer | ||
|
||
import ( | ||
"io" | ||
"time" | ||
) | ||
|
||
// NilSource is a no-op sentinel Source. | ||
var NilSource = nilSource{ | ||
ReadCloser: io.NopCloser(nil), | ||
} | ||
|
||
type nilSource struct { | ||
io.ReadCloser | ||
} | ||
|
||
func (nilSource) Name() string { | ||
return "" | ||
} | ||
|
||
func (nilSource) CreatedAt() time.Time { | ||
return time.Time{} | ||
} | ||
|
||
func (nilSource) ModifiedAt() time.Time { | ||
return time.Time{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package sourcer | ||
|
||
import ( | ||
"io" | ||
"time" | ||
) | ||
|
||
// Source is a data source that can be read from. | ||
type Source interface { | ||
io.ReadCloser | ||
Name() string | ||
|
||
CreatedAt() time.Time | ||
ModifiedAt() time.Time | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters