Skip to content

Commit

Permalink
update golangci-lint + misc code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-darkly committed Jun 4, 2020
1 parent e085a3a commit 0e0d49f
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 38 deletions.
40 changes: 34 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,44 @@ run:
tests: false

linters:
enable-all: true
disable:
enable:
- bodyclose
- deadcode
- depguard
- dupl
- errcheck
- goconst
- gochecknoglobals
- gochecknoinits
- goconst
- gomnd
- maligned
- wsl
- gocritic
- gocyclo
- godox
- gofmt
- goimports
- golint
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- lll
- megacheck
- misspell
- nakedret
- nolintlint
- prealloc
- staticcheck
- structcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
fast: false

linter-settings:
linters-settings:
gofmt:
simplify: false

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

GOLANGCI_LINT_VERSION=v1.23.7
GOLANGCI_LINT_VERSION=v1.27.0

LINTER=./bin/golangci-lint
LINTER_VERSION_FILE=./bin/.golangci-lint-version-$(GOLANGCI_LINT_VERSION)
Expand Down
2 changes: 1 addition & 1 deletion ldlog/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ type levelLogger struct {
overrideLogger bool
}

var nullLog = levelLogger{enabled: false}
var nullLog = levelLogger{enabled: false} //nolint:gochecknoglobals

// NewDisabledLoggers returns a Loggers instance that will never generate output.
func NewDisabledLoggers() Loggers {
Expand Down
27 changes: 27 additions & 0 deletions ldvalue/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ldvalue

// string literals should be defined here if they are referenced in multiple files

const nullAsJSON = "null"

// ValueType is defined in value_base.go

const (
// NullType describes a null value. Note that the zero value of ValueType is NullType, so the
// zero of Value is a null value.
NullType ValueType = iota
// BoolType describes a boolean value.
BoolType ValueType = iota
// NumberType describes a numeric value. JSON does not have separate types for int and float, but
// you can convert to either.
NumberType ValueType = iota
// StringType describes a string value.
StringType ValueType = iota
// ArrayType describes an array value.
ArrayType ValueType = iota
// ObjectType describes an object (a.k.a. map).
ObjectType ValueType = iota
// RawType describes a json.RawMessage value. This value will not be parsed or interpreted as
// any other data type, and can be accessed only by calling AsRaw().
RawType ValueType = iota
)
4 changes: 2 additions & 2 deletions ldvalue/optional_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (o OptionalString) MarshalJSON() ([]byte, error) {
if o.hasValue {
return json.Marshal(o.value)
}
return []byte("null"), nil
return []byte(nullAsJSON), nil
}

// UnmarshalJSON parses an OptionalString from JSON.
Expand All @@ -128,7 +128,7 @@ func (o *OptionalString) UnmarshalJSON(data []byte) error {
case 'n':
// Note that since Go 1.5, comparing a string to string([]byte) is optimized so it
// does not actually create a new string from the byte slice.
if string(data) == "null" {
if string(data) == nullAsJSON {
*o = OptionalString{}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions ldvalue/optional_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestOptionalStringAsStringer(t *testing.T) {
func TestOptionalStringMarshalling(t *testing.T) {
bytes, err := json.Marshal(OptionalString{})
assert.NoError(t, err)
assert.Equal(t, "null", string(bytes))
assert.Equal(t, nullAsJSON, string(bytes))

bytes, err = json.Marshal(NewOptionalString(`a "good" string`))
assert.NoError(t, err)
Expand All @@ -77,7 +77,7 @@ func TestOptionalStringMarshalling(t *testing.T) {

func TestOptionalStringUnmarshalling(t *testing.T) {
var o OptionalString
err := json.Unmarshal([]byte("null"), &o)
err := json.Unmarshal([]byte(nullAsJSON), &o)
assert.NoError(t, err)
assert.False(t, o.IsDefined())

Expand Down
22 changes: 1 addition & 21 deletions ldvalue/value_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,11 @@ type Value struct {
// ValueType indicates which JSON type is contained in a Value.
type ValueType int

const (
// NullType describes a null value. Note that the zero value of ValueType is NullType, so the
// zero of Value is a null value.
NullType ValueType = iota
// BoolType describes a boolean value.
BoolType ValueType = iota
// NumberType describes a numeric value. JSON does not have separate types for int and float, but
// you can convert to either.
NumberType ValueType = iota
// StringType describes a string value.
StringType ValueType = iota
// ArrayType describes an array value.
ArrayType ValueType = iota
// ObjectType describes an object (a.k.a. map).
ObjectType ValueType = iota
// RawType describes a json.RawMessage value. This value will not be parsed or interpreted as
// any other data type, and can be accessed only by calling AsRaw().
RawType ValueType = iota
)

// String returns the name of the value type.
func (t ValueType) String() string {
switch t {
case NullType:
return "null"
return nullAsJSON
case BoolType:
return "bool"
case NumberType:
Expand Down
2 changes: 1 addition & 1 deletion ldvalue/value_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestValueTypes(t *testing.T) {
assert.Equal(t, "null", NullType.String())
assert.Equal(t, nullAsJSON, NullType.String())
assert.Equal(t, "bool", BoolType.String())
assert.Equal(t, "number", NumberType.String())
assert.Equal(t, "string", StringType.String())
Expand Down
6 changes: 3 additions & 3 deletions ldvalue/value_serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (v Value) JSONString() string {
// converting between byte arrays and strings.
switch v.valueType {
case NullType:
return "null"
return nullAsJSON
case BoolType:
if v.boolValue {
return "true"
Expand All @@ -28,7 +28,7 @@ func (v Value) JSONString() string {
return strconv.FormatFloat(v.numberValue, 'f', -1, 64)
}
// For all other types, we rely on our custom marshaller.
bytes, _ := json.Marshal(v) //nolint:gosec // see comment below
bytes, _ := json.Marshal(v)
// It shouldn't be possible for marshalling to fail, because Value can only contain
// JSON-compatible types. But if it somehow did fail, bytes will be nil and we'll return
// an empty tring.
Expand All @@ -43,7 +43,7 @@ func (v Value) JSONString() string {
func (v Value) MarshalJSON() ([]byte, error) {
switch v.valueType {
case NullType:
return []byte("null"), nil
return []byte(nullAsJSON), nil
case BoolType:
if v.boolValue {
return []byte("true"), nil
Expand Down
2 changes: 1 addition & 1 deletion ldvalue/value_serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestJsonMarshalUnmarshal(t *testing.T) {
value Value
json string
}{
{Null(), "null"},
{Null(), nullAsJSON},
{Bool(true), "true"},
{Bool(false), "false"},
{Int(1), "1"},
Expand Down

0 comments on commit 0e0d49f

Please sign in to comment.