Skip to content

Commit

Permalink
[Go] add fury go util and ci tests (#950)
Browse files Browse the repository at this point in the history
* add fury go util

* fury mod

* fix go mod name

* add util test

* add golang ci

* fix go mod

* add go sum

* add license for golang

* config golang header style

* fix header style
  • Loading branch information
chaokunyang authored Oct 5, 2023
1 parent 71f2531 commit 383165b
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ jobs:
run: ./ci/run_ci.sh install_bazel
- name: Run Python CI
run: ./ci/run_ci.sh python
go:
name: Golang CI
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.13', '1.18' ]
steps:
- uses: actions/checkout@v4
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Display Go version
run: go version
- name: Run Golang CI
run: ./ci/run_ci.sh go
lint:
name: Code Style Check
runs-on: ubuntu-latest
Expand Down
8 changes: 7 additions & 1 deletion ci/run_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,13 @@ case $1 in
exit $testcode
fi
echo "Executing fury python tests succeeds"
;;
;;
go)
echo "Executing fury go tests for go"
cd "$ROOT/go/fury"
go test -v
echo "Executing fury go tests succeeds"
;;
format)
echo "Install format tools"
pip install black==22.1.0 flake8==3.9.1 flake8-quotes flake8-bugbear click==8.0.2
Expand Down
22 changes: 22 additions & 0 deletions go/fury/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2023 The Fury Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

module github.com/alipay/fury/go/fury

go 1.13

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.7.0
)
12 changes: 12 additions & 0 deletions go/fury/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
125 changes: 125 additions & 0 deletions go/fury/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2023 The Fury Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fury

import (
"bytes"
"encoding/binary"
"reflect"
"time"
"unsafe"
)

var nativeEndian binary.ByteOrder

func init() {
buf := [2]byte{}
*(*uint16)(unsafe.Pointer(&buf[0])) = uint16(0xABCD)

switch buf {
case [2]byte{0xCD, 0xAB}:
nativeEndian = binary.LittleEndian
case [2]byte{0xAB, 0xCD}:
nativeEndian = binary.BigEndian
default:
panic("Could not determine native endianness.")
}
}

var emptyByteSlice []byte

// unsafeGetBytes converts string to byte slice.
func unsafeGetBytes(s string) []byte {
if len(s) == 0 {
return emptyByteSlice
}
return (*[0x7fff0000]byte)(unsafe.Pointer(
(*reflect.StringHeader)(unsafe.Pointer(&s)).Data),
)[:len(s):len(s)]
}

func SnakeCase(camel string) string {
var buf bytes.Buffer
for _, c := range camel {
if 'A' <= c && c <= 'Z' {
// just convert [A-Z] to _[a-z]
if buf.Len() > 0 {
buf.WriteRune('_')
}
buf.WriteRune(c - 'A' + 'a')
} else {
buf.WriteRune(c)
}
}
return buf.String()
}

// Float32bits returns the IEEE 754 binary representation of f,
// with the sign bit of f and the result in the same bit position.
// Float32bits(Float32frombits(x)) == x.
func Float32bits(f float32) uint32 { return *(*uint32)(unsafe.Pointer(&f)) }

// Float32frombits returns the floating-point number corresponding
// to the IEEE 754 binary representation b, with the sign bit of b
// and the result in the same bit position.
// Float32frombits(Float32bits(x)) == x.
func Float32frombits(b uint32) float32 { return *(*float32)(unsafe.Pointer(&b)) }

// Float64bits returns the IEEE 754 binary representation of f,
// with the sign bit of f and the result in the same bit position,
// and Float64bits(Float64frombits(x)) == x.
func Float64bits(f float64) uint64 { return *(*uint64)(unsafe.Pointer(&f)) }

// Float64frombits returns the floating-point number corresponding
// to the IEEE 754 binary representation b, with the sign bit of b
// and the result in the same bit position.
// Float64frombits(Float64bits(x)) == x.
func Float64frombits(b uint64) float64 { return *(*float64)(unsafe.Pointer(&b)) }

// Integer limit values.
const (
intSize = 32 << (^uint(0) >> 63) // 32 or 64

MaxInt = 1<<(intSize-1) - 1
MinInt = -1 << (intSize - 1)
MaxInt8 = 1<<7 - 1
MinInt8 = -1 << 7
MaxInt16 = 1<<15 - 1
MinInt16 = -1 << 15
MaxInt32 = 1<<31 - 1
MinInt32 = -1 << 31
MaxInt64 = 1<<63 - 1
MinInt64 = -1 << 63
MaxUint = 1<<intSize - 1
MaxUint8 = 1<<8 - 1
MaxUint16 = 1<<16 - 1
MaxUint32 = 1<<32 - 1
MaxUint64 = 1<<64 - 1
)

// GetUnixMicro returns t as a Unix time, the number of microseconds elapsed since
// January 1, 1970 UTC. The result is undefined if the Unix time in
// microseconds cannot be represented by an int64 (a date before year -290307 or
// after year 294246). The result does not depend on the location associated
// with t.
func GetUnixMicro(t time.Time) int64 {
return int64(t.Unix())*1e6 + int64(t.Nanosecond())/1e3
}

// CreateTimeFromUnixMicro returns the local Time corresponding to the given Unix time,
// usec microseconds since January 1, 1970 UTC.
func CreateTimeFromUnixMicro(usec int64) time.Time {
return time.Unix(usec/1e6, (usec%1e6)*1e3)
}
42 changes: 42 additions & 0 deletions go/fury/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2023 The Fury Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fury

import (
"github.com/stretchr/testify/require"
"testing"
"time"
)

func TestSnake(t *testing.T) {
require.Equal(t, "a_bcd_efg_hij", SnakeCase("aBcdEfgHij"))
require.Equal(t, "a_bcd_efg_hij", SnakeCase("ABcdEfgHij"))
require.Equal(t, "a_b_c_d_efg_hij", SnakeCase("ABCDEfgHij"))
require.Equal(t, SnakeCase("ToSnake"), "to_snake")
require.Equal(t, SnakeCase("toSnake"), "to_snake")
require.Equal(t, SnakeCase("to_snake"), "to_snake")
require.Equal(t, SnakeCase("AbcAbcAbc"), "abc_abc_abc")
require.Equal(t, SnakeCase("ABC"), "a_b_c")
}

func TestTime(t *testing.T) {
t1 := time.Now()
ts := GetUnixMicro(t1)
t2 := CreateTimeFromUnixMicro(ts)
require.Equal(t, t1.Second(), t2.Second())
// Micro doesn't preserve Nanosecond precision.
require.Equal(t, t1.Nanosecond()/1000, t2.Nanosecond()/1000)
require.WithinDuration(t, t1, t2, 1000)
}
5 changes: 5 additions & 0 deletions licenserc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ excludes = [
"javascript/packages/hps/src/v8-fast-api-calls.h",
"javascript/packages/fury/lib/murmurHash3.ts",
"src/fury/thirdparty/MurmurHash3.*",
"go/fury/go.sum",
]

[mapping.DOUBLESLASH_STYLE]
extensions = ['go']
filenames = ['go.mod']

[properties]
inceptionYear = 2023
copyrightOwner = "The Fury Authors"

0 comments on commit 383165b

Please sign in to comment.