Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 46 additions & 17 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,40 @@ name: go
on:
pull_request:
branches: [main]
paths-ignore:
- '**.md'
- '.github/workflows/**'
- 'LICENSE'
- '.gitignore'
- 'docs/**'
push:
branches: [main]
paths-ignore:
- '**.md'
- '.github/workflows/**'
- 'LICENSE'
- '.gitignore'
- 'docs/**'
tags:
- 'v*'

jobs:
lint:
id: lint
check_changes:
runs-on: ubuntu-latest
outputs:
should_run: ${{ steps.check.outputs.should_run }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Check for code changes
id: check
run: |
git diff --name-only HEAD^ HEAD > changes.txt
echo "Changed files:"
cat changes.txt

# Check if there are any changes outside of doc/workflow files
if grep -qv -E '(\.md$|\.github/workflows/|LICENSE|\.gitignore|docs/)' changes.txt; then
echo "Code changes detected, should run tests"
echo "should_run=true" >> $GITHUB_OUTPUT
else
echo "Only doc changes, skipping tests"
echo "should_run=false" >> $GITHUB_OUTPUT
fi

lint:
needs: check_changes
runs-on: ubuntu-latest
if: ${{ needs.check_changes.outputs.should_run == 'true' }}
name: Linters for Go
steps:
- name: checkout code into Go module dir
Expand All @@ -39,15 +52,23 @@ jobs:
GOBIN: /tmp/.bin
run: make lint

skip_lint:
needs: check_changes
runs-on: ubuntu-latest
if: ${{ needs.check_changes.outputs.should_run == 'false' }}
steps:
- name: Skip lint
run: echo "Skipping lint for documentation-only changes"

test:
id: test
needs: check_changes
if: ${{ needs.check_changes.outputs.should_run == 'true' }}
runs-on: ${{ matrix.platform }}
strategy:
fail-fast: false
matrix:
go: ['1.24.x']
platform: [ubuntu-latest, macos-latest]

name: Unit tests on Go ${{ matrix.go }} ${{ matrix.platform }}
steps:
- name: checkout code into module directory
Expand All @@ -68,4 +89,12 @@ jobs:
- name: Run unit tests
env:
GOBIN: /tmp/.bin
run: go test -v ./...
run: go test -v ./...

skip_test:
needs: check_changes
runs-on: ubuntu-latest
if: ${{ needs.check_changes.outputs.should_run == 'false' }}
steps:
- name: Skip tests
run: echo "Skipping tests for documentation-only changes"
22 changes: 13 additions & 9 deletions gobdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
// its Data field. The path field is unexported and contains the path to the
// file where the database is stored.
//
// The Gobdb type parameter T is constrained by the 'any' type constraint, meaning
// it can be any Go type.
// The Gobdb type parameter T is constrained by the 'comparable' type
// constraint, meaning it can be any comparable Go type. See
// https://go.dev/ref/spec#Comparison_operators for more information.
//
// Fields:
//
Expand All @@ -30,11 +31,11 @@ type Gobdb[T comparable] struct {
}

// Open is a generic function that opens a file at the specified path and
// decodes its contents using the gob decoder. The type of the data in the
// decodes its contents using gob decoder. The type of the data in the
// file should be specified as the type argument T when calling this function.
//
// The type parameter T is constrained by the 'any' type constraint, meaning
// it can be any Go type.
// The type parameter T is constrained by the 'comparable' type constraint, meaning
// it can be any comparable Go type.
//
// If the decoding fails, it returns an error. If the decoding is successful,
// it returns a Gobdb object with the decoded data.
Expand Down Expand Up @@ -130,11 +131,10 @@ func (db *Gobdb[T]) Sync() error {
return nil
}


func (db *Gobdb[T]) Delete(vals ...T) error {
var datatmp []T
// we keep the db.Data values that are not in vals.
for _, v := range db.Data {
for _, v := range db.Data {
if !slices.Contains(vals, v) {
datatmp = append(datatmp, v)
}
Expand All @@ -143,5 +143,9 @@ func (db *Gobdb[T]) Delete(vals ...T) error {
return db.Sync()
}


// TODO: dumper use a decoder on `any` and writes to io.Writer.
// DeleteAll deletes all values from the database and syncs the changes to
// the file. It clears the Data slice and writes an empty slice to the file.
func (db *Gobdb[T]) DeleteAll() error {
db.Data = []T{}
return db.Sync()
}
34 changes: 34 additions & 0 deletions gobdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,37 @@ func TestDelete(t *testing.T) {
t.Error(cmp.Diff(want, got))
}
}

func TestDeleteAll(t *testing.T) {
t.Parallel()

path := t.TempDir() + "/db.gobdb"
db, err := gobdb.Open[string](path)
if err != nil {
t.Fatalf("unable to open db: %s", err)
}
want := []string{"barbara", "victor", "walter", "walter"}
err = db.Add(want...)
if err != nil {
t.Errorf("unable to add data %v: %s", want, err)
}

// extra validation, check everything, check the state, tests should not
// depend on other tests.
want = []string{"barbara", "victor", "walter", "walter"}
got := db.List()
if !cmp.Equal(want, got) {
t.Fatal(cmp.Diff(want, got))
}

err = db.DeleteAll()
if err != nil {
t.Fatalf("unable to delete all: %s", err)
}

want = []string{}
got = db.List()
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
}
Loading