Skip to content

Commit

Permalink
add ci and readme badges (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
quasilyte authored Nov 5, 2023
1 parent 6c30632 commit 28adb95
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This file adheres to the YAML5 style.
{
"name": "Go",
"on": ["push", "pull_request"],
"jobs": {
"build": {
"name": "Build",
"runs-on": "ubuntu-latest",
"steps": [
{
"name": "Set up Go 1.19",
"uses": "actions/setup-go@v1",
"with": {"go-version": 1.19},
"id": "go",
},
{"name": "Check out code into the Go module directory", "uses": "actions/checkout@v1"},
{"name": "Linter", "run": "make ci-lint"},
{"name": "Test", "run": "make test"},
],
},
},
}
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
GOPATH_DIR=`go env GOPATH`

test:
go test -v -count=3 .

ci-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH_DIR)/bin v1.54.2
$(GOPATH_DIR)/bin/golangci-lint run ./...
@echo "everything is OK"

lint:
golangci-lint run ./...
@echo "everything is OK"

.PHONY: ci-lint lint test
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# gdata

![Build Status](https://github.com/quasilyte/gdata/workflows/Go/badge.svg)
[![PkgGoDev](https://pkg.go.dev/badge/mod/github.com/quasilyte/gdata)](https://pkg.go.dev/mod/github.com/quasilyte/gdata)

A gamedata package that provides a convenient cross-platform storage for games.

Some examples of such gamedata that you might want to store:
Expand Down
13 changes: 13 additions & 0 deletions gdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Manager struct {
type dataManagerImpl interface {
SaveData(itemKey string, data []byte) error
LoadData(itemKey string) ([]byte, error)
DeleteData(itemKey string) error
DataExists(itemKey string) bool
DataPath(itemKey string) string
}
Expand Down Expand Up @@ -74,6 +75,18 @@ func (m *Manager) LoadItem(itemKey string) ([]byte, error) {
return m.impl.LoadData(itemKey)
}

// DeleteItem removes the data associated with the itemKey.
//
// Be careful with this function: it removes the data permanently.
// There is no way to undo it.
//
// Trying to delete a non-existing itemKey is not an error.
//
// The returned error is usually a file operation error.
func (m *Manager) DeleteItem(itemKey string) error {
return m.impl.DeleteData(itemKey)
}

// ItemExists reports whether the itemKey was saved before.
// An existing key will result in a non-nil data being read with LoadItem().
func (m *Manager) ItemExists(itemKey string) bool {
Expand Down
8 changes: 8 additions & 0 deletions gdata_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func (m *dataManager) SaveData(itemKey string, data []byte) error {
return os.WriteFile(m.DataPath(itemKey), data, 0o666)
}

func (m *dataManager) DeleteData(itemKey string) error {
itemPath := m.DataPath(itemKey)
if !fileExists(itemPath) {
return nil
}
return os.Remove(itemPath)
}

func (m *dataManager) LoadData(itemKey string) ([]byte, error) {
itemPath := m.DataPath(itemKey)
if !fileExists(itemPath) {
Expand Down
5 changes: 5 additions & 0 deletions gdata_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func (m *dataManager) SaveData(itemKey string, data []byte) error {
return nil
}

func (m *dataManager) DeleteData(itemKey string) error {
js.Global().Get("localStorage").Call("removeItem", m.DataPath(itemKey))
return nil
}

func (m *dataManager) LoadData(itemKey string) ([]byte, error) {
result := js.Global().Get("localStorage").Call("getItem", m.DataPath(itemKey))
if result.IsNull() {
Expand Down
56 changes: 56 additions & 0 deletions gdata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package gdata_test

import (
"bytes"
"testing"

"github.com/quasilyte/gdata"
)

func TestSaveLoad(t *testing.T) {
m, err := gdata.Open(gdata.Config{
AppName: "gdata_test",
})
if err != nil {
t.Fatal(err)
}

const testItemKey = "testitem.txt"

// Deleting a potentially non-existing item.
// For a second run it acts like a state reset function.
if err := m.DeleteItem(testItemKey); err != nil {
t.Fatalf("delete should never result in an error here (got %v)", err)
}

if m.ItemExists(testItemKey) {
t.Fatalf("%s item should not exist yet", testItemKey)
}

data := []byte("example data")
if err := m.SaveItem(testItemKey, data); err != nil {
t.Fatalf("error saving %s data", testItemKey)
}

if !m.ItemExists(testItemKey) {
t.Fatalf("%s item should exist after a successful save operation", testItemKey)
}

loadedData, err := m.LoadItem(testItemKey)
if err != nil {
t.Fatalf("loading %s error: %v", testItemKey, err)
}

if !bytes.Equal(data, loadedData) {
t.Fatalf("saved and loaded data mismatch:\nwant: %q\n have: %q", string(data), string(loadedData))
}

// Now we're deleting an existing item.
if err := m.DeleteItem(testItemKey); err != nil {
t.Fatalf("delete should never result in an error here (got %v)", err)
}

if m.ItemExists(testItemKey) {
t.Fatalf("%s item should not exist after a successful delete operation", testItemKey)
}
}
8 changes: 8 additions & 0 deletions gdata_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func (m *dataManager) SaveData(itemKey string, data []byte) error {
return os.WriteFile(m.DataPath(itemKey), data, 0o666)
}

func (m *dataManager) DeleteData(itemKey string) error {
itemPath := m.DataPath(itemKey)
if !fileExists(itemPath) {
return nil
}
return os.Remove(itemPath)
}

func (m *dataManager) LoadData(itemKey string) ([]byte, error) {
itemPath := m.DataPath(itemKey)
if !fileExists(itemPath) {
Expand Down
8 changes: 8 additions & 0 deletions gdata_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ func (m *dataManager) SaveData(itemKey string, data []byte) error {
return os.WriteFile(m.DataPath(itemKey), data, 0o666)
}

func (m *dataManager) DeleteData(itemKey string) error {
itemPath := m.DataPath(itemKey)
if !fileExists(itemPath) {
return nil
}
return os.Remove(itemPath)
}

func (m *dataManager) LoadData(itemKey string) ([]byte, error) {
itemPath := m.DataPath(itemKey)
if !fileExists(itemPath) {
Expand Down

0 comments on commit 28adb95

Please sign in to comment.