From dbb5736ffc9a399b2bbdf741912f6c4a283a87da Mon Sep 17 00:00:00 2001 From: Walter Vargas Date: Fri, 28 Mar 2025 23:11:13 +0000 Subject: [PATCH 1/4] Add DeleteAll() --- gobdb.go | 22 +++++++++++++--------- gobdb_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/gobdb.go b/gobdb.go index 7e085af..6a62384 100644 --- a/gobdb.go +++ b/gobdb.go @@ -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: // @@ -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. @@ -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) } @@ -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() +} diff --git a/gobdb_test.go b/gobdb_test.go index 988c5a4..d374833 100644 --- a/gobdb_test.go +++ b/gobdb_test.go @@ -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)) + } +} From 453995d252e81fc8059baafd8fe3e3bdf314141b Mon Sep 17 00:00:00 2001 From: Walter Vargas Date: Fri, 28 Mar 2025 23:14:16 +0000 Subject: [PATCH 2/4] Fix workflow --- .github/workflows/go.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5733dea..89bdf0b 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -21,8 +21,7 @@ on: - 'v*' jobs: - lint: - id: lint + lint: runs-on: ubuntu-latest name: Linters for Go steps: @@ -40,7 +39,6 @@ jobs: run: make lint test: - id: test runs-on: ${{ matrix.platform }} strategy: fail-fast: false From 9da1979ec5e8ea230495b71d2eb07c923c03d7ed Mon Sep 17 00:00:00 2001 From: Walter Vargas Date: Fri, 28 Mar 2025 23:18:50 +0000 Subject: [PATCH 3/4] Fix go workflow --- .github/workflows/go.yml | 57 +++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 89bdf0b..448e0b3 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -3,26 +3,39 @@ 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: + 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 + 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 @@ -38,7 +51,17 @@ 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: + needs: check_changes + if: ${{ needs.check_changes.outputs.should_run == 'true' }} runs-on: ${{ matrix.platform }} strategy: fail-fast: false @@ -66,4 +89,12 @@ jobs: - name: Run unit tests env: GOBIN: /tmp/.bin - run: go test -v ./... \ No newline at end of file + 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" \ No newline at end of file From f7f612e2c37e33a22d6117e937bd11413b7526ce Mon Sep 17 00:00:00 2001 From: Walter Vargas Date: Fri, 28 Mar 2025 23:23:36 +0000 Subject: [PATCH 4/4] Fix --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 448e0b3..043f07b 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -31,6 +31,7 @@ jobs: else echo "Only doc changes, skipping tests" echo "should_run=false" >> $GITHUB_OUTPUT + fi lint: needs: check_changes @@ -68,7 +69,6 @@ jobs: 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