Skip to content

Commit

Permalink
add Boolean type (#190)
Browse files Browse the repository at this point in the history
This Boolean type is required in order to correctly serialize/de-serialize map keys that contain bools in accordance with the conjure spec.
  • Loading branch information
tabboud authored Aug 25, 2020
1 parent 45113e6 commit d0eff66
Show file tree
Hide file tree
Showing 69 changed files with 20,019 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ workflows:
requires:
- binary-verify

# boolean
- godel/verify:
name: boolean-verify
<<: *checkout-path
include-tests: true
executor:
name: go/golang
version: 1.13.4
owner-repo: palantir/pkg/boolean
- godel/test:
name: boolean-test-go-1.12
<<: *checkout-path
executor:
name: go/golang
version: 1.12.13
owner-repo: palantir/pkg/boolean
requires:
- boolean-verify

# bytesbuffers
- godel/verify:
name: bytesbuffers-verify
Expand Down
28 changes: 28 additions & 0 deletions boolean/boolean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2020 Palantir Technologies. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package boolean

import (
"strconv"
)

// Boolean is an alias for bool which implements serialization matching the conjure wire specification.
// ref: https://github.com/palantir/conjure/blob/master/docs/spec/wire.md
type Boolean bool

// UnmarshalText implements encoding.TextUnmarshaler (used by encoding/json and others).
func (b *Boolean) UnmarshalText(data []byte) error {
bo, err := strconv.ParseBool(string(data))
if err != nil {
return err
}
*b = Boolean(bo)
return nil
}

// MarshalText implements encoding.TextMarshaler (used by encoding/json and others).
func (b Boolean) MarshalText() ([]byte, error) {
return []byte(strconv.FormatBool(bool(b))), nil
}
78 changes: 78 additions & 0 deletions boolean/boolean_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2020 Palantir Technologies. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package boolean

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
)

func TestBoolean_Marshal(t *testing.T) {
for _, tc := range []struct {
name string
input bool
output []byte
}{
{
name: "false input",
input: false,
output: []byte(`"false"`),
},
{
name: "true input",
input: true,
output: []byte(`"true"`),
},
} {
t.Run(tc.name, func(t *testing.T) {
out, err := json.Marshal(Boolean(tc.input))
require.NoError(t, err)
require.Equal(t, out, tc.output)
})
}
}

func TestBoolean_Unmarshal(t *testing.T) {
for _, tc := range []struct {
name string
input []byte
output Boolean
expectErr bool
}{
{
name: "nil input",
input: nil,
output: false,
expectErr: true,
},
{
name: "empty input",
input: []byte(`""`),
output: false,
expectErr: true,
},
{
name: "false input",
input: []byte(`"false"`),
output: false,
expectErr: false,
},
{
name: "true input",
input: []byte(`"true"`),
output: true,
expectErr: false,
},
} {
t.Run(tc.name, func(t *testing.T) {
var b Boolean
err := json.Unmarshal(tc.input, &b)
require.Equal(t, tc.expectErr, err != nil)
require.Equal(t, b, tc.output)
})
}
}
8 changes: 8 additions & 0 deletions boolean/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/palantir/pkg/boolean

go 1.13

require (
github.com/palantir/pkg v1.0.1
github.com/stretchr/testify v1.4.0
)
13 changes: 13 additions & 0 deletions boolean/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/palantir/pkg v1.0.1 h1:ZbGUcc14N7xcZSY9cehQoiHHTm/BAZO5RJdlsNEtSbk=
github.com/palantir/pkg v1.0.1/go.mod h1:Eo6Jl0UXfT+65sLXJOcU9duu0WPvKsWFXCb0dE5VWZs=
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.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
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.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
5 changes: 5 additions & 0 deletions boolean/godel/config/check-plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
checks:
golint:
filters:
- value: "should have comment or be unexported"
- value: "or a comment on this block"
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions boolean/godel/config/godel.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
distributionURL=https://palantir.bintray.com/releases/com/palantir/godel/godel/2.25.0/godel-2.25.0.tgz
distributionSHA256=f9a58181cfc1c709e9ac4a763bce386fb4e0c5ad24a635120b7d592c60e10206
18 changes: 18 additions & 0 deletions boolean/godel/config/godel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins:
resolvers:
- https://palantir.bintray.com/releases/{{GroupPath}}/{{Product}}/{{Version}}/{{Product}}-{{Version}}-{{OS}}-{{Arch}}.tgz
plugins:
- locator:
id: com.palantir.godel-mod-plugin:mod-plugin:1.0.1
checksums:
darwin-amd64: df22922bacfe4e4e7c255607a0aace176205f04ae001f3746276fcfab1780e01
linux-amd64: a2697b3d504bb37c2fd8831a66c7014927a6d94e4dfb9765b4764354370a1ab6
environment:
GO111MODULE: "on"
GOFLAGS: "-mod=vendor"
exclude:
names:
- "\\..+"
- "vendor"
paths:
- "godel"
4 changes: 4 additions & 0 deletions boolean/godel/config/license-plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
header: |
// Copyright (c) {{YEAR}} Palantir Technologies. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
Empty file.
Loading

0 comments on commit d0eff66

Please sign in to comment.