-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from kaleido-io/map-omit
Do not include the ",omitempty" in the JSON field name
- Loading branch information
Showing
10 changed files
with
233 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright © 2022 Kaleido, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// 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 fftypes | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"math/big" | ||
"strconv" | ||
|
||
"github.com/hyperledger/firefly-common/pkg/i18n" | ||
) | ||
|
||
// FFuint64 on the API are serialized as Base10 strings, and can be parsed from multiple bases | ||
// (very similar to fftypes.FFBigInt, but limited to a 64bit unsigned integer) | ||
type FFuint64 uint64 | ||
|
||
func (i FFuint64) MarshalText() ([]byte, error) { | ||
// Represent as base 10 string in Marshalled JSON | ||
return []byte(strconv.FormatUint(uint64(i), 10)), nil | ||
} | ||
|
||
func (i *FFuint64) UnmarshalJSON(b []byte) error { | ||
var val interface{} | ||
if err := json.Unmarshal(b, &val); err != nil { | ||
return i18n.WrapError(context.Background(), err, i18n.MsgBigIntParseFailed, b) | ||
} | ||
switch val := val.(type) { | ||
case string: | ||
bi, ok := new(big.Int).SetString(val, 0) | ||
if !ok { | ||
return i18n.NewError(context.Background(), i18n.MsgBigIntParseFailed, b) | ||
} | ||
*i = FFuint64(bi.Int64()) | ||
return nil | ||
case float64: | ||
*i = FFuint64(val) | ||
return nil | ||
default: | ||
return i18n.NewError(context.Background(), i18n.MsgBigIntParseFailed, b) | ||
} | ||
} | ||
|
||
func (i *FFuint64) Uint64() uint64 { | ||
if i == nil { | ||
return 0 | ||
} | ||
return uint64(*i) | ||
} | ||
|
||
// FFint64 on the API are serialized as Base10 strings, and can be parsed from multiple bases | ||
// (very similar to fftypes.FFBigInt, but limited to a 64bit signed integer) | ||
type FFint64 int64 | ||
|
||
func (i FFint64) MarshalText() ([]byte, error) { | ||
// Represent as base 10 string in Marshalled JSON | ||
return []byte(strconv.FormatInt(int64(i), 10)), nil | ||
} | ||
|
||
func (i *FFint64) UnmarshalJSON(b []byte) error { | ||
var val interface{} | ||
if err := json.Unmarshal(b, &val); err != nil { | ||
return i18n.WrapError(context.Background(), err, i18n.MsgBigIntParseFailed, b) | ||
} | ||
switch val := val.(type) { | ||
case string: | ||
bi, ok := new(big.Int).SetString(val, 0) | ||
if !ok { | ||
return i18n.NewError(context.Background(), i18n.MsgBigIntParseFailed, b) | ||
} | ||
*i = FFint64(bi.Int64()) | ||
return nil | ||
case float64: | ||
*i = FFint64(val) | ||
return nil | ||
default: | ||
return i18n.NewError(context.Background(), i18n.MsgBigIntParseFailed, b) | ||
} | ||
} | ||
|
||
func (i *FFint64) Int64() int64 { | ||
if i == nil { | ||
return 0 | ||
} | ||
return int64(*i) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright © 2022 Kaleido, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// 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 fftypes | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFFInt64JSON(t *testing.T) { | ||
|
||
var myStruct struct { | ||
Field1 FFint64 `json:"field1"` | ||
Field2 *FFint64 `json:"field2"` | ||
Field3 *FFint64 `json:"field3"` | ||
Field4 *FFint64 `json:"field4"` | ||
} | ||
|
||
jsonVal := []byte(`{ | ||
"field1": -111111, | ||
"field2": 2222.22, | ||
"field3": "333333", | ||
"field4": "0xfeedBEEF" | ||
}`) | ||
|
||
err := json.Unmarshal(jsonVal, &myStruct) | ||
assert.NoError(t, err) | ||
assert.Equal(t, int64(-111111), myStruct.Field1.Int64()) | ||
assert.Equal(t, int64(2222), myStruct.Field2.Int64()) | ||
assert.Equal(t, int64(333333), myStruct.Field3.Int64()) | ||
assert.Equal(t, int64(4276993775), myStruct.Field4.Int64()) | ||
|
||
jsonValSerialized, err := json.Marshal(&myStruct) | ||
|
||
assert.NoError(t, err) | ||
assert.JSONEq(t, `{ | ||
"field1": "-111111", | ||
"field2": "2222", | ||
"field3": "333333", | ||
"field4": "4276993775" | ||
}`, string(jsonValSerialized)) | ||
|
||
var ffi *FFuint64 | ||
assert.Equal(t, uint64(0), ffi.Uint64()) | ||
err = ffi.UnmarshalJSON([]byte(`"bad string`)) | ||
assert.Regexp(t, "FF00104", err) | ||
err = ffi.UnmarshalJSON([]byte(`"!!! not a number"`)) | ||
assert.Regexp(t, "FF00104", err) | ||
err = ffi.UnmarshalJSON([]byte(`{}`)) | ||
assert.Regexp(t, "FF00104", err) | ||
} | ||
|
||
func TestFFUint64JSON(t *testing.T) { | ||
|
||
var myStruct struct { | ||
Field1 FFuint64 `json:"field1"` | ||
Field2 *FFuint64 `json:"field2"` | ||
Field3 *FFuint64 `json:"field3"` | ||
Field4 *FFuint64 `json:"field4"` | ||
} | ||
|
||
jsonVal := []byte(`{ | ||
"field1": 111111, | ||
"field2": 2222.22, | ||
"field3": "333333", | ||
"field4": "0xfeedBEEF" | ||
}`) | ||
|
||
err := json.Unmarshal(jsonVal, &myStruct) | ||
assert.NoError(t, err) | ||
assert.Equal(t, uint64(111111), myStruct.Field1.Uint64()) | ||
assert.Equal(t, uint64(2222), myStruct.Field2.Uint64()) | ||
assert.Equal(t, uint64(333333), myStruct.Field3.Uint64()) | ||
assert.Equal(t, uint64(4276993775), myStruct.Field4.Uint64()) | ||
|
||
jsonValSerialized, err := json.Marshal(&myStruct) | ||
|
||
assert.NoError(t, err) | ||
assert.JSONEq(t, `{ | ||
"field1": "111111", | ||
"field2": "2222", | ||
"field3": "333333", | ||
"field4": "4276993775" | ||
}`, string(jsonValSerialized)) | ||
|
||
var ffi *FFint64 | ||
assert.Equal(t, int64(0), ffi.Int64()) | ||
err = ffi.UnmarshalJSON([]byte(`"bad string`)) | ||
assert.Regexp(t, "FF00104", err) | ||
err = ffi.UnmarshalJSON([]byte(`"!!! not a number"`)) | ||
assert.Regexp(t, "FF00104", err) | ||
err = ffi.UnmarshalJSON([]byte(`{}`)) | ||
assert.Regexp(t, "FF00104", err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters