-
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.
add jsonx,defaultx,toAny of stringx.
- Loading branch information
Showing
12 changed files
with
610 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# gdkits | ||
|
||
the development toolkits of go |
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,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 | ||
} |
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,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 | ||
} |
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,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 |
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,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) | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |
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,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 | ||
} |
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,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 |
Oops, something went wrong.