Added to package to convert to pointers (#18)

* Added to package to convert to pointers
Also updated deps and removed agrea/ptr

Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>
This commit is contained in:
Alessandro (Ale) Segala 2022-07-26 11:30:19 -07:00 committed by GitHub
parent 15a34b12af
commit 247f04d375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 173 additions and 79 deletions

View File

@ -21,11 +21,11 @@ jobs:
name: Build ${{ matrix.target_os }}_${{ matrix.target_arch }} binaries
runs-on: ${{ matrix.os }}
env:
GOVER: 1.16
GOVER: "1.18"
GOOS: ${{ matrix.target_os }}
GOARCH: ${{ matrix.target_arch }}
GOPROXY: https://proxy.golang.org
GOLANGCI_LINT_VER: v1.31
GOLANGCI_LINT_VER: v1.45.2
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
@ -51,9 +51,10 @@ jobs:
uses: actions/checkout@v2
- name: Run golangci-lint
if: matrix.target_arch == 'amd64' && matrix.target_os == 'linux'
uses: golangci/golangci-lint-action@v3.1.0
uses: golangci/golangci-lint-action@v3.2.0
with:
version: ${{ env.GOLANGCI_LINT_VER }}
skip-cache: true
- name: Run make go.mod check-diff
if: matrix.target_arch != 'arm'
run: make go.mod check-diff

View File

@ -247,7 +247,29 @@ linters:
- exhaustive
- noctx
- gci
issues:
exclude:
# staticcheck
- 'SA1019: package github.com/golang/protobuf/proto is deprecated: Use the "google.golang.org/protobuf/proto" package instead'
- golint
- tparallel
- paralleltest
- wrapcheck
- tagliatelle
- ireturn
- exhaustivestruct
- errchkjson
- contextcheck
- gomoddirectives
- godot
- cyclop
- varnamelen
- gosec
- errorlint
- forcetypeassert
- ifshort
- maintidx
- nilnil
- predeclared
- tenv
- thelper
- wastedassign
- containedctx
- gosimple
- forbidigo

View File

@ -58,10 +58,7 @@ func Decode(input interface{}, output interface{}) error {
}
// nolint:cyclop
func decodeString(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
func decodeString(f reflect.Type, t reflect.Type, data any) (any, error) {
if t.Kind() == reflect.String && f.Kind() != reflect.String {
return fmt.Sprintf("%v", data), nil
}
@ -78,7 +75,7 @@ func decodeString(
return nil, errors.Errorf("expected string: got %s", reflect.TypeOf(data))
}
var result interface{}
var result any
var decoder StringDecoder
if t.Implements(typeStringDecoder) {

View File

@ -20,10 +20,10 @@ import (
"testing"
"time"
"github.com/agrea/ptr"
"github.com/stretchr/testify/assert"
"github.com/dapr/kit/config"
"github.com/dapr/kit/ptr"
)
type testConfig struct { // nolint: maligned
@ -94,37 +94,37 @@ func TestDecode(t *testing.T) {
tests := map[string]interface{}{
"primitive values": map[string]interface{}{
"int": -9999,
"intPtr": ptr.Int(-9999),
"intPtr": ptr.Of(-9999),
"int64": -1234,
"int64Ptr": ptr.Int64(-12345),
"int64Ptr": ptr.Of(-12345),
"int32": -5678,
"int32Ptr": ptr.Int64(-5678),
"int32Ptr": ptr.Of(-5678),
"int16": -9012,
"int16Ptr": ptr.Int32(-9012),
"int16Ptr": ptr.Of(-9012),
"int8": -128,
"int8Ptr": ptr.Int8(-128),
"int8Ptr": ptr.Of(-128),
"uint": 9999,
"uintPtr": ptr.Uint(9999),
"uintPtr": ptr.Of(9999),
"uint64": 1234,
"uint64Ptr": ptr.Uint64(1234),
"uint64Ptr": ptr.Of(1234),
"uint32": 5678,
"uint32Ptr": ptr.Uint64(5678),
"uint32Ptr": ptr.Of(5678),
"uint16": 9012,
"uint16Ptr": ptr.Uint64(9012),
"uint16Ptr": ptr.Of(9012),
"byte": 255,
"bytePtr": ptr.Byte(255),
"bytePtr": ptr.Of(255),
"float64": 1234.5,
"float64Ptr": ptr.Float64(1234.5),
"float64Ptr": ptr.Of(1234.5),
"float32": 6789.5,
"float32Ptr": ptr.Float64(6789.5),
"float32Ptr": ptr.Of(6789.5),
"bool": true,
"boolPtr": ptr.Bool(true),
"boolPtr": ptr.Of(true),
"duration": 5 * time.Second,
"durationPtr": durationPtr(5 * time.Second),
"durationPtr": ptr.Of(5 * time.Second),
"time": timeVal,
"timePtr": timePtr(timeVal),
"timePtr": ptr.Of(timeVal),
"string": 1234,
"stringPtr": ptr.String("1234"),
"stringPtr": ptr.Of("1234"),
"decoded": "unlimited",
"decodedPtr": "unlimited",
"nested": map[string]interface{}{
@ -288,18 +288,6 @@ func TestDecodeErrors(t *testing.T) {
}
}
func durationPtr(value time.Duration) *time.Duration {
return &value
}
func timePtr(value time.Time) *time.Time {
return &value
}
func decodedPtr(value Decoded) *Decoded {
return &value
}
func getTimeVal() time.Time {
timeVal, _ := time.Parse(time.RFC3339, "2021-01-02T15:04:05-07:00")
@ -311,39 +299,39 @@ func getExpected() testConfig {
return testConfig{
Int: -9999,
IntPtr: ptr.Int(-9999),
IntPtr: ptr.Of(-9999),
Int64: -1234,
Int64Ptr: ptr.Int64(-12345),
Int64Ptr: ptr.Of(int64(-12345)),
Int32: -5678,
Int32Ptr: ptr.Int32(-5678),
Int32Ptr: ptr.Of(int32(-5678)),
Int16: -9012,
Int16Ptr: ptr.Int16(-9012),
Int16Ptr: ptr.Of(int16(-9012)),
Int8: -128,
Int8Ptr: ptr.Int8(-128),
Int8Ptr: ptr.Of(int8(-128)),
Uint: 9999,
UintPtr: ptr.Uint(9999),
UintPtr: ptr.Of(uint(9999)),
Uint64: 1234,
Uint64Ptr: ptr.Uint64(1234),
Uint64Ptr: ptr.Of(uint64(1234)),
Uint32: 5678,
Uint32Ptr: ptr.Uint32(5678),
Uint32Ptr: ptr.Of(uint32(5678)),
Uint16: 9012,
Uint16Ptr: ptr.Uint16(9012),
Uint16Ptr: ptr.Of(uint16(9012)),
Byte: 255,
BytePtr: ptr.Byte(255),
BytePtr: ptr.Of(byte(255)),
Float64: 1234.5,
Float64Ptr: ptr.Float64(1234.5),
Float64Ptr: ptr.Of(1234.5),
Float32: 6789.5,
Float32Ptr: ptr.Float32(6789.5),
Float32Ptr: ptr.Of(float32(6789.5)),
Bool: true,
BoolPtr: ptr.Bool(true),
BoolPtr: ptr.Of(true),
Duration: 5 * time.Second,
DurationPtr: durationPtr(5 * time.Second),
DurationPtr: ptr.Of(5 * time.Second),
Time: timeVal,
TimePtr: timePtr(timeVal),
TimePtr: ptr.Of(timeVal),
String: "1234",
StringPtr: ptr.String("1234"),
StringPtr: ptr.Of("1234"),
Decoded: -1,
DecodedPtr: decodedPtr(-1),
DecodedPtr: ptr.Of(Decoded(-1)),
Nested: nested{
Integer: 1234,
String: "5678",

18
go.mod
View File

@ -1,12 +1,18 @@
module github.com/dapr/kit
go 1.16
go 1.18
require (
github.com/agrea/ptr v0.0.0-20180711073057-77a518d99b7b
github.com/cenkalti/backoff/v4 v4.1.1
github.com/mitchellh/mapstructure v1.4.1
github.com/cenkalti/backoff/v4 v4.1.3
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.0
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

27
go.sum
View File

@ -1,25 +1,26 @@
github.com/agrea/ptr v0.0.0-20180711073057-77a518d99b7b h1:WMhlIaJkDgEQSVJQM06YV+cYUl1r5OY5//ijMXJNqtA=
github.com/agrea/ptr v0.0.0-20180711073057-77a518d99b7b/go.mod h1:Tie46d3UWzXpj+Fh9+DQTyaUxEpFBPOLXrnx7nxlKRo=
github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ=
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
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/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 h1:BpfhmLKZf+SjVanKKhCgf3bg+511DmU9eDQTen7LLbY=
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -28,7 +28,7 @@ type daprLogger struct {
logger *logrus.Entry
}
var DaprVersion string = "unknown"
var DaprVersion = "unknown"
func newDaprLogger(name string) *daprLogger {
newLogger := logrus.New()

View File

@ -52,7 +52,8 @@ func (o *Options) SetAppID(id string) {
// AttachCmdFlags attaches log options to command flags.
func (o *Options) AttachCmdFlags(
stringVar func(p *string, name string, value string, usage string),
boolVar func(p *bool, name string, value bool, usage string)) {
boolVar func(p *bool, name string, value bool, usage string),
) {
if stringVar != nil {
stringVar(
&o.OutputLevel,

32
ptr/of.go Normal file
View File

@ -0,0 +1,32 @@
/*
Copyright 2021 The Dapr 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.
*/
// Based on https://github.com/Azure/azure-sdk-for-go/blob/sdk/azcore/v1.1.1/sdk/azcore/to/to.go
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package ptr
// Of returns a pointer to the provided value.
func Of[T any](v T) *T {
return &v
}
// SliceOfPtrs returns a slice of *T from the specified values.
func SliceOfPtrs[T any](vv ...T) []*T {
slc := make([]*T, len(vv))
for i := range vv {
slc[i] = Of(vv[i])
}
return slc
}

46
ptr/of_test.go Normal file
View File

@ -0,0 +1,46 @@
/*
Copyright 2021 The Dapr 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.
*/
// Based on https://github.com/Azure/azure-sdk-for-go/blob/sdk/azcore/v1.1.1/sdk/azcore/to/to_test.go
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package ptr
import (
"testing"
)
func TestPtr(t *testing.T) {
b := true
pb := Of(b)
if pb == nil {
t.Fatal("unexpected nil conversion")
}
if *pb != b {
t.Fatalf("got %v, want %v", *pb, b)
}
}
func TestSliceOfPtrs(t *testing.T) {
arr := SliceOfPtrs[int]()
if len(arr) != 0 {
t.Fatal("expected zero length")
}
arr = SliceOfPtrs(1, 2, 3, 4, 5)
for i, v := range arr {
if *v != i+1 {
t.Fatal("values don't match")
}
}
}