Skip to content

Commit

Permalink
feat: add GitHub Actions workflow for Go CI and remove unused byteuti…
Browse files Browse the repository at this point in the history
…l package
  • Loading branch information
deancn committed Nov 17, 2024
1 parent d4fbcf8 commit 9de2561
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 90 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Go
on: [push]

permissions:
contents: read

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable

- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60

- name: Run golangci-lint
run: golangci-lint run

- name: Test
#run: echo go test -v .
run: echo go test ./... -count=1 -v
33 changes: 0 additions & 33 deletions byteutil/byte.go

This file was deleted.

31 changes: 0 additions & 31 deletions byteutil/random_string.go

This file was deleted.

22 changes: 9 additions & 13 deletions utilkit/byte.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package utilkit
// Refer: https://gist.github.com/yakuter/c0df0f4253ea639529f3589e99dc940b

import (
"reflect"
"unsafe"
)

Expand All @@ -16,18 +15,15 @@ func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

// s2b converts string to a byte slice without memory allocation.
// StringToBytes converts a string to a byte slice without memory allocation.
//
// Note it may break if string and/or slice header will change
// in the future go versions.
func StringToBytes(s string) (b []byte) {
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))

//nolint: govet
sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
// Warning: This function is unsafe and should be used with caution. Modifying
// the resulting byte slice may lead to undefined behavior if the original
// string is immutable. Ensure compatibility with future Go versions.
func StringToBytes(s string) []byte {
// Get the pointer to the string data
data := unsafe.StringData(s)

return b
// Convert to a byte slice using unsafe.Slice
return unsafe.Slice(data, len(s))
}
9 changes: 4 additions & 5 deletions utilkit/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package utilkit
import (
"archive/tar"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -17,7 +16,6 @@ const (

func Exists(path string) bool {
_, err := os.Stat(path)

return !os.IsNotExist(err)
}

Expand All @@ -39,14 +37,15 @@ func EnsureFileExists(path string) {

EnsureFolderExists(filepath.Dir(path))

err := ioutil.WriteFile(path, nil, FilePerm644)
file, err := os.Create(path)
if err != nil {
log.Fatal("file not exists, err: ", err)
}
file.Close()
}

func ReadFile(path string) ([]byte, error) {
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
log.Fatal("read file fail, err: ", err)
return nil, err
Expand All @@ -58,7 +57,7 @@ func ReadFile(path string) ([]byte, error) {
func WriteFile(path string, content string) error {
EnsureFileExists(path)

err := ioutil.WriteFile(path, StringToBytes(content), FilePerm644)
err := os.WriteFile(path, []byte(content), FilePerm644)
if err != nil {
return err
}
Expand Down
7 changes: 2 additions & 5 deletions utilkit/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package utilkit
import (
"bytes"
"io"
"io/ioutil"
"text/template"

"github.com/airdb/toolbox/byteutil"
)

func TemplateGenerateString(str string, data interface{}) (string, error) {
Expand All @@ -24,12 +21,12 @@ func TemplateGenerateString(str string, data interface{}) (string, error) {
}

func TemplateGenerateFileFromReader(reader io.Reader, dstPath string, data interface{}) error {
b, err := ioutil.ReadAll(reader)
b, err := io.ReadAll(reader)
if err != nil {
return err
}

content, err := TemplateGenerateString(byteutil.BytesToString(b), data)
content, err := TemplateGenerateString(BytesToString(b), data)
if err != nil {
return err
}
Expand Down
17 changes: 14 additions & 3 deletions utilkit/time_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package utilkit_test

import (
"strings"
"testing"
"time"

"github.com/airdb/toolbox/utilkit"
)

func TestGetNowLong(t *testing.T) {
utilkit.GetNowLong()
ts := utilkit.GetNowLong()
if ts == "" {
t.Errorf("Expected a valid timestamp, got %s", ts)
}
}

func TestGetYearMonthDay(t *testing.T) {
utilkit.GetYearMonthDay()
today := utilkit.GetYearMonthDay()
if !strings.Contains(today, "-") {
t.Errorf("Expected a valid date string, got %s", today)
}

}

func TestGetTimeRFC(t *testing.T) {
now := time.Now().Unix()
utilkit.GetTimeRFC(now)
rfcTime := utilkit.GetTimeRFC(now)
if _, err := time.Parse(time.RFC3339, rfcTime); err != nil {
t.Errorf("Expected valid RFC3339 time format, got %s", rfcTime)
}
}

0 comments on commit 9de2561

Please sign in to comment.