Skip to content

Commit 4a4788a

Browse files
author
Dean Karn
authored
Add unsafe (#4)
- Add unsafe package with BytesToString and StringToBytes functions - Fix lint error on LimitReader error, had to bump to v6 because of this. - updated to use GitHub Action for CI
1 parent 4569437 commit 4a4788a

File tree

10 files changed

+96
-48
lines changed

10 files changed

+96
-48
lines changed

.github/workflows/go.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Lint & Test
2+
on: [push, pull_request]
3+
jobs:
4+
test:
5+
strategy:
6+
matrix:
7+
go-version: [1.14.x,1.13.x]
8+
platform: [ubuntu-latest, macos-latest, windows-latest]
9+
runs-on: ${{ matrix.platform }}
10+
steps:
11+
- name: Install Go
12+
uses: actions/setup-go@v1
13+
with:
14+
go-version: ${{ matrix.go-version }}
15+
16+
- name: Priming Cache
17+
uses: actions/cache@v1
18+
with:
19+
path: ~/go/pkg/mod
20+
key: ${{ runner.os }}-v1-go-${{ hashFiles('**/go.sum') }}
21+
restore-keys: |
22+
${{ runner.os }}-v1-go-
23+
24+
- name: Checkout code
25+
uses: actions/checkout@v2
26+
27+
- name: Lint
28+
if: matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.14.x'
29+
run: |
30+
export PATH=$PATH:$(go env GOPATH)/bin # temporary fix. See https://github.com/actions/setup-go/issues/14
31+
go get -u golang.org/x/lint/golint
32+
golint -set_exit_status ./...
33+
34+
- name: Test
35+
run: go test ./...

.travis.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

Makefile

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
GOCMD=GO111MODULE=on go
22

3-
linters-install:
4-
@golangci-lint --version >/dev/null 2>&1 || { \
5-
echo "installing linting tools..."; \
6-
$(GOCMD) get github.com/golangci/golangci-lint/cmd/golangci-lint; \
7-
}
8-
9-
lint: linters-install
10-
golangci-lint run
11-
123
test:
134
$(GOCMD) test -cover -race ./...
145

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# pkg
2-
![Project status](https://img.shields.io/badge/version-4.0.0-green.svg)
2+
![Project status](https://img.shields.io/badge/version-5.0.0-green.svg)
33
[![Build Status](https://travis-ci.org/go-playground/pkg.svg?branch=master)](https://travis-ci.org/go-playground/pkg)
44
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pkg/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pkg?branch=master)
5-
[![GoDoc](https://godoc.org/github.com/go-playground/pkg?status.svg)](https://godoc.org/github.com/go-playground/pkg)
5+
[![GoDoc](https://godoc.org/github.com/go-playground/pkg?status.svg)](https://pkg.go.dev/mod/github.com/go-playground/pkg/v5)
66
![License](https://img.shields.io/dub/l/vibe-d.svg)
77

8-
pkg extends the core go packages with missing or additional functionality built in. All packages correspond to the std go package name with an additional suffix of `ext` to avoid naming conflicts.
8+
pkg extends the core Go packages with missing or additional functionality built in. All packages correspond to the std go package name with an additional suffix of `ext` to avoid naming conflicts.
99

1010
Motivation
1111
----------
12-
This is a place to put comman reusable code that is not quite a library but extends upon the core library or it's failings.
12+
This is a place to put common reusable code that is not quite a library but extends upon the core library, or it's failings.
1313

1414
How to Contribute
1515
------

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
module github.com/go-playground/pkg/v4
1+
module github.com/go-playground/pkg/v5
22

33
require (
4+
github.com/go-playground/assert v1.2.1 // indirect
45
github.com/go-playground/assert/v2 v2.0.1
5-
github.com/go-playground/form/v4 v4.0.0
6+
github.com/go-playground/form/v4 v4.1.1
67
)
78

8-
go 1.13
9+
go 1.14

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBY
44
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
55
github.com/go-playground/form/v4 v4.0.0 h1:vUKi2K1Hqlc4rpBc0tCclgs9zSfbY5yMKsL106db/eY=
66
github.com/go-playground/form/v4 v4.0.0/go.mod h1:bodWfd97U9PVMZFcDsbVzSbQQTtaWrebnTwQtWjSW1M=
7+
github.com/go-playground/form/v4 v4.1.1 h1:1T9lGt3WRHuDwT5uwx7RYQZfxVwWZhF0DC1ovKyNnWY=
8+
github.com/go-playground/form/v4 v4.1.1/go.mod h1:q1a2BY+AQUUzhl6xA/6hBetay6dEIhMHjgvJiGo6K7U=

io/limit_reader.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ import (
66
)
77

88
var (
9-
// LimitedReaderEOF is an error returned by LimitedReader to give feedback to the fact that we did not hit an
9+
// ErrLimitedReaderEOF is an error returned by LimitedReader to give feedback to the fact that we did not hit an
1010
// EOF of the Reader but hit the limit imposed by the LimitedReader.
11-
LimitedReaderEOF = errors.New("LimitedReader EOF: limit reached")
11+
ErrLimitedReaderEOF = errors.New("LimitedReader EOF: limit reached")
1212
)
1313

1414
// LimitReader returns a LimitedReader that reads from r
15-
// but stops with LimitedReaderEOF after n bytes.
15+
// but stops with ErrLimitedReaderEOF after n bytes.
1616
func LimitReader(r io.Reader, n int64) *LimitedReader {
1717
return &LimitedReader{R: r, N: n}
1818
}
1919

2020
// A LimitedReader reads from R but limits the amount of
2121
// data returned to just N bytes. Each call to Read
2222
// updates N to reflect the new amount remaining.
23-
// Read returns LimitedReaderEOF when N <= 0 or when the underlying R returns EOF.
23+
// Read returns ErrLimitedReaderEOF when N <= 0 or when the underlying R returns EOF.
2424
// Unlike the std io.LimitedReader this provides feedback
2525
// that the limit was reached through the returned error.
2626
type LimitedReader struct {
@@ -38,7 +38,7 @@ func (l *LimitedReader) Read(p []byte) (n int, err error) {
3838
return
3939
}
4040
if l.N < 0 {
41-
return n, LimitedReaderEOF
41+
return n, ErrLimitedReaderEOF
4242
}
4343
return
4444
}

net/http/helpers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111
"path/filepath"
1212
"strings"
1313

14-
ioext "github.com/go-playground/pkg/v4/io"
14+
ioext "github.com/go-playground/pkg/v5/io"
1515
)
1616

1717
// QueryParamsOption represents the options for including query parameters during Decode helper functions
1818
type QueryParamsOption uint8
1919

20+
// QueryParamsOption's
2021
const (
2122
QueryParams QueryParamsOption = iota
2223
NoQueryParams

unsafe/conversions.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package unsafeext
2+
3+
import (
4+
"reflect"
5+
"unsafe"
6+
)
7+
8+
// BytesToString converts an array of bytes into a string without allocating.
9+
// The byte slice passed to this function is not to be used after this call as it's unsafe; you have been warned.
10+
func BytesToString(b []byte) string {
11+
return *(*string)(unsafe.Pointer(&b))
12+
}
13+
14+
// StringToBytes converts an existing string into an []byte without allocating.
15+
// The string passed to this functions is not to be used again after this call as it's unsafe; you have been warned.
16+
func StringToBytes(s string) (b []byte) {
17+
strHdr := (*reflect.StringHeader)(unsafe.Pointer(&s))
18+
sliceHdr := (*reflect.SliceHeader)(unsafe.Pointer(&b))
19+
sliceHdr.Data = strHdr.Data
20+
sliceHdr.Cap = strHdr.Len
21+
sliceHdr.Len = strHdr.Len
22+
return
23+
}

unsafe/conversions_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package unsafeext
2+
3+
import "testing"
4+
5+
func TestBytesToString(t *testing.T) {
6+
b := []byte{'g', 'o', '-', 'p', 'l', 'a', 'y', 'g', 'r', 'o', 'u', 'n', 'd'}
7+
s := BytesToString(b)
8+
expected := string(b)
9+
if s != expected {
10+
t.Fatalf("expected '%s' got '%s'", expected, s)
11+
}
12+
}
13+
14+
func TestStringToBytes(t *testing.T) {
15+
s := "go-playground"
16+
b := StringToBytes(s)
17+
18+
if string(b) != s {
19+
t.Fatalf("expected '%s' got '%s'", s, string(b))
20+
}
21+
}

0 commit comments

Comments
 (0)