Skip to content

Commit

Permalink
Return richer struct on the history
Browse files Browse the repository at this point in the history
  • Loading branch information
gjhenrique committed Jan 24, 2023
1 parent 07d2dd6 commit 4e90e5d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 11 additions & 3 deletions store/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ type HistoryStore struct {
Dir string
}

type HistoryEntry struct {
Entry []byte
Position int
}

func formatEntry(entryKey []byte, n int) []byte {
e := append(entryKey, "\t"...)
return append(e, strconv.Itoa(n)...)
Expand All @@ -25,10 +30,10 @@ func (h *HistoryStore) fileName(modeKey string) string {
return filepath.Join(h.Dir, fmt.Sprintf("%s_history", modeKey))
}

func (h *HistoryStore) ListEntries(modeKey string) ([][]byte, error) {
func (h *HistoryStore) ListEntries(modeKey string) ([]*HistoryEntry, error) {
fileContent, err := ioutil.ReadFile(h.fileName(modeKey))
fields := bytes.Split(fileContent, []byte("\n"))
s := make([][]byte, 0)
s := make([]*HistoryEntry, 0)

if err != nil {
if errors.Is(err, os.ErrNotExist) {
Expand All @@ -38,11 +43,14 @@ func (h *HistoryStore) ListEntries(modeKey string) ([][]byte, error) {
}
}

var i int
for _, field := range fields {
arr := bytes.Split(field, []byte("\t"))
first := arr[0]
if len(first) > 0 {
s = append(s, first)
entry := HistoryEntry{Entry: first, Position: i}
s = append(s, &entry)
i += i
}
}

Expand Down
6 changes: 3 additions & 3 deletions store/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func TestOrderIsMaintained(t *testing.T) {
require.NoError(t, err)

require.Len(t, entries, 3)
require.Equal(t, string(entries[0]), "b")
require.Equal(t, string(entries[1]), "a")
require.Equal(t, string(entries[2]), "c")
require.Equal(t, string(entries[0].Entry), "b")
require.Equal(t, string(entries[1].Entry), "a")
require.Equal(t, string(entries[2].Entry), "c")
}

func TestEmptyArrayWhenFileDoesNotExist(t *testing.T) {
Expand Down

0 comments on commit 4e90e5d

Please sign in to comment.