Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"bom.go",
"discard_go15.go",
],
importpath = "go-common/app/admin/main/member/model/bom",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_xtest",
srcs = ["bom_test.go"],
tags = ["automanaged"],
deps = [
"//app/admin/main/member/model/bom:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,39 @@
// Package bom is used to clean up UTF-8 Byte Order Marks.
package bom
import (
"bufio"
"io"
)
const (
bom0 = 0xef
bom1 = 0xbb
bom2 = 0xbf
)
// Clean returns b with the 3 byte BOM stripped off the front if it is present.
// If the BOM is not present, then b is returned.
func Clean(b []byte) []byte {
if len(b) >= 3 &&
b[0] == bom0 &&
b[1] == bom1 &&
b[2] == bom2 {
return b[3:]
}
return b
}
// NewReader returns an io.Reader that will skip over initial UTF-8 byte order marks.
func NewReader(r io.Reader) io.Reader {
buf := bufio.NewReader(r)
b, err := buf.Peek(3)
if err != nil {
// not enough bytes
return buf
}
if b[0] == bom0 && b[1] == bom1 && b[2] == bom2 {
discardBytes(buf, 3)
}
return buf
}

View File

@@ -0,0 +1,80 @@
package bom_test
import (
"bytes"
"io/ioutil"
"testing"
"go-common/app/admin/main/member/model/bom"
"github.com/stretchr/testify/assert"
)
var testCases = []struct {
Input []byte
Expected []byte
}{
{
Input: nil,
Expected: nil,
},
{
Input: []byte{},
Expected: []byte{},
},
{
Input: []byte{0xef},
Expected: []byte{0xef},
},
{
Input: []byte{0xef, 0xbb},
Expected: []byte{0xef, 0xbb},
},
{
Input: []byte{0xef, 0xbb, 0xbf},
Expected: []byte{},
},
{
Input: []byte{0xef, 0xbb, 0xbf, 0x41, 0x42, 0x43},
Expected: []byte{0x41, 0x42, 0x43},
},
{
Input: []byte{0xef, 0xbb, 0x41, 0x42, 0x43},
Expected: []byte{0xef, 0xbb, 0x41, 0x42, 0x43},
},
{
Input: []byte{0xef, 0x41, 0x42, 0x43},
Expected: []byte{0xef, 0x41, 0x42, 0x43},
},
{
Input: []byte{0x41, 0x42, 0x43},
Expected: []byte{0x41, 0x42, 0x43},
},
}
func TestClean(t *testing.T) {
assert := assert.New(t)
for _, tc := range testCases {
output := bom.Clean(tc.Input)
assert.Equal(tc.Expected, output)
}
}
func TestReader(t *testing.T) {
assert := assert.New(t)
for _, tc := range testCases {
// An input value of nil works differently to the Clean function.
// In this case it results in an empty buffer, not nil.
expected := tc.Expected
if tc.Input == nil {
expected = []byte{}
}
r1 := bytes.NewReader(tc.Input)
r2 := bom.NewReader(r1)
output, err := ioutil.ReadAll(r2)
assert.NoError(err)
assert.Equal(expected, output)
}
}

View File

@@ -0,0 +1,12 @@
// +build !go1.5
package bom
import "bufio"
func discardBytes(buf *bufio.Reader, n int) {
// cannot use the buf.Discard method as it was introduced in Go 1.5
for i := 0; i < n; i++ {
buf.ReadByte()
}
}

View File

@@ -0,0 +1,10 @@
// +build go1.5
package bom
import "bufio"
func discardBytes(buf *bufio.Reader, n int) {
// the Discard method was introduced in Go 1.5
buf.Discard(n)
}