Skip to content

Commit

Permalink
add jsonx,defaultx,toAny of stringx.
Browse files Browse the repository at this point in the history
  • Loading branch information
qicz committed May 7, 2022
1 parent 5836577 commit dfc2b85
Show file tree
Hide file tree
Showing 12 changed files with 610 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# gdkits

the development toolkits of go
31 changes: 31 additions & 0 deletions gox/defaultx/boolx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2022, OpeningO
* All rights reserved.
*
* 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 defaultx

import (
"log"
)

// DefaultIfError if error use false
func DefaultIfError(value bool, err error) bool {
if err != nil {
log.Fatalln(err)
return false
}
return value
}
56 changes: 56 additions & 0 deletions gox/defaultx/number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2022, OpeningO
* All rights reserved.
*
* 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 defaultx

import "log"

// DefaultComplexIfError if error use defaultValue
func DefaultComplexIfError(err error, value complex128, defaultValue complex128) complex128 {
if err != nil {
log.Fatalln(err)
return defaultValue
}
return value
}

// DefaultFloat64IfError if error use defaultValue
func DefaultFloat64IfError(err error, value float64, defaultValue float64) float64 {
if err != nil {
log.Fatalln(err)
return defaultValue
}
return value
}

// DefaultIntIfError if error use defaultValue
func DefaultIntIfError(err error, value int, defaultValue int) int {
if err != nil {
log.Fatalln(err)
return defaultValue
}
return value
}

// DefaultUint64IfError if error use defaultValue
func DefaultUint64IfError(err error, value uint64, defaultValue uint64) uint64 {
if err != nil {
log.Fatalln(err)
return defaultValue
}
return value
}
18 changes: 18 additions & 0 deletions gox/encodingx/encodingx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2022, OpeningO
* All rights reserved.
*
* 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 encodingx
45 changes: 45 additions & 0 deletions gox/encodingx/jsonx/jsonx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2022, OpeningO
* All rights reserved.
*
* 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 jsonx

import "encoding/json"

// ToJsonBytes convert any to json bytes
// wrap json.Marshal
func ToJsonBytes(v any) ([]byte, error) {
return json.Marshal(v)
}

// ToJsonString convert any to json string
// wrap json.Marshal
func ToJsonString(v any) (string, error) {
marshal, err := json.Marshal(v)
return string(marshal), err
}

// JsonBytesToAny convert json bytes to any
// wrap json.Unmarshal
func JsonBytesToAny(bytes []byte, v any) error {
return json.Unmarshal(bytes, v)
}

// JsonStringToAny convert json bytes to any
// wrap json.Unmarshal
func JsonStringToAny(str string, v any) error {
return json.Unmarshal([]byte(str), v)
}
151 changes: 151 additions & 0 deletions gox/encodingx/jsonx/jsonx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright (c) 2022, OpeningO
* All rights reserved.
*
* 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 jsonx

import (
"reflect"
"testing"
)

type user struct {
Name string `json:"name"`
Age int `json:"age"`
}

var u = user{
Name: "qicz",
Age: 123,
}

func TestJsonBytesToAny(t *testing.T) {
type args struct {
bytes []byte
v any
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "json bytes to any",
args: args{
bytes: []byte("{\"name\":\"qicz\",\"age\":123}"),
v: &user{},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := JsonBytesToAny(tt.args.bytes, tt.args.v); (err != nil) != tt.wantErr {
t.Errorf("JsonBytesToAny() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestJsonStringToAny(t *testing.T) {
type args struct {
str string
v any
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "json string to any",
args: args{
str: "{\"name\":\"qicz\",\"age\":123}",
v: &u,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := JsonStringToAny(tt.args.str, tt.args.v); (err != nil) != tt.wantErr {
t.Errorf("JsonStringToAny() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestToJsonBytes(t *testing.T) {
type args struct {
v any
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{
name: "any to json bytes",
args: args{v: u},
want: []byte("{\"name\":\"qicz\",\"age\":123}"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ToJsonBytes(tt.args.v)
if (err != nil) != tt.wantErr {
t.Errorf("ToJsonBytes() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToJsonBytes() got = %v, want %v", got, tt.want)
}
})
}
}

func TestToJsonString(t *testing.T) {
type args struct {
v any
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "any to json string",
args: args{v: u},
want: "{\"name\":\"qicz\",\"age\":123}",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ToJsonString(tt.args.v)
if (err != nil) != tt.wantErr {
t.Errorf("ToJsonString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ToJsonString() got = %v, want %v", got, tt.want)
}
})
}
}
28 changes: 28 additions & 0 deletions gox/errorsx/errorsx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2022, OpeningO
* All rights reserved.
*
* 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 errorsx

import "log"

// LogError call log.Fatalln
func LogError(err error) error {
if err != nil {
log.Fatalln(err)
}
return err
}
18 changes: 18 additions & 0 deletions gox/number/number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2022, OpeningO
* All rights reserved.
*
* 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 number
Loading

0 comments on commit dfc2b85

Please sign in to comment.