Skip to content

Commit eff44cd

Browse files
committed
✨ feat: add unify functions #5
1 parent 9b94deb commit eff44cd

File tree

11 files changed

+2143
-2
lines changed

11 files changed

+2143
-2
lines changed

const.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package unify4go
2+
3+
import (
4+
"regexp"
5+
"unicode/utf8"
6+
)
7+
8+
var (
9+
// Len is an alias for the utf8.RuneCountInString function, which returns the number of runes
10+
// (Unicode code points) in the given string. This function treats erroneous and short
11+
// encodings as single runes of width 1 byte. It is useful for determining the
12+
// length of a string in terms of its Unicode characters, rather than bytes,
13+
// allowing for accurate character counting in UTF-8 encoded strings.
14+
Len = utf8.RuneCountInString
15+
16+
// RegexpDupSpaces is a precompiled regular expression that matches one or more consecutive
17+
// whitespace characters (including spaces, tabs, and newlines). This can be used for tasks
18+
// such as normalizing whitespace in strings by replacing multiple whitespace characters
19+
// with a single space, or for validating string formats where excessive whitespace should
20+
// be trimmed or removed.
21+
RegexpDupSpaces = regexp.MustCompile(`\s+`)
22+
)

example/strings_test.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
module github.com/sivaosorg/unify4go.git
1+
module github.com/sivaosorg/unify4go
22

33
go 1.23.1
4+
5+
require github.com/json-iterator/go v1.1.12
6+
7+
require (
8+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
9+
github.com/modern-go/reflect2 v1.0.2 // indirect
10+
)

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
5+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
6+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
7+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
8+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
9+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
10+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
11+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
14+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
15+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

json.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package unify4go
2+
3+
import (
4+
jsonI "github.com/json-iterator/go"
5+
)
6+
7+
var _json = jsonI.ConfigCompatibleWithStandardLibrary
8+
9+
// Marshal converts a Go value into its JSON byte representation.
10+
//
11+
// This function marshals the input value `v` using the json-iterator library.
12+
// The resulting JSON data is returned as a byte slice. If there is an error
13+
// during marshalling, it returns the error.
14+
//
15+
// Parameters:
16+
// - `v`: The Go value to be marshalled into JSON.
17+
//
18+
// Returns:
19+
// - A byte slice containing the JSON representation of the input value.
20+
// - An error if the marshalling fails.
21+
//
22+
// Example:
23+
// jsonData, err := Marshal(myStruct)
24+
func Marshal(v interface{}) ([]byte, error) {
25+
return _json.Marshal(v)
26+
}
27+
28+
// MarshalIndent converts a Go value to its JSON string representation with indentation.
29+
//
30+
// This function marshals the input value `v` into a formatted JSON string,
31+
// allowing for easy readability by including a specified prefix and indentation.
32+
// It returns the resulting JSON byte slice or an error if marshalling fails.
33+
//
34+
// Parameters:
35+
// - `v`: The Go value to be marshalled into JSON.
36+
// - `prefix`: A string that will be prefixed to each line of the output JSON.
37+
// - `indent`: A string used for indentation (typically a series of spaces or a tab).
38+
//
39+
// Returns:
40+
// - A byte slice containing the formatted JSON representation of the input value.
41+
// - An error if the marshalling fails.
42+
//
43+
// Example:
44+
// jsonIndented, err := MarshalIndent(myStruct, "", " ")
45+
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
46+
return _json.MarshalIndent(v, prefix, indent)
47+
}
48+
49+
// MarshalToString converts a Go value to its JSON string representation.
50+
//
51+
// This function utilizes the json-iterator library to marshal the input value `v`
52+
// into a JSON string. If the marshalling is successful, it returns the resulting
53+
// JSON string. If an error occurs during the process, it returns an error.
54+
//
55+
// Parameters:
56+
// - `v`: The Go value to be marshalled into JSON.
57+
//
58+
// Returns:
59+
// - A string containing the JSON representation of the input value.
60+
// - An error if the marshalling fails.
61+
//
62+
// Example:
63+
// jsonString, err := MarshalToString(myStruct)
64+
func MarshalToString(v interface{}) (string, error) {
65+
return _json.MarshalToString(v)
66+
}
67+
68+
// Unmarshal parses JSON-encoded data and stores the result in the value pointed to by `v`.
69+
//
70+
// This function uses the json-iterator library to unmarshal JSON data
71+
// (given as a byte slice) into the specified Go value `v`. If the unmarshalling
72+
// is successful, it populates the value `v`. If an error occurs, it returns the error.
73+
//
74+
// Parameters:
75+
// - `data`: A byte slice containing JSON data to be unmarshalled.
76+
// - `v`: A pointer to the Go value where the unmarshalled data will be stored.
77+
//
78+
// Returns:
79+
// - An error if the unmarshalling fails.
80+
//
81+
// Example:
82+
// err := Unmarshal(jsonData, &myStruct)
83+
func Unmarshal(data []byte, v interface{}) error {
84+
return _json.Unmarshal(data, v)
85+
}
86+
87+
// UnmarshalFromString parses JSON-encoded string and stores the result in the value pointed to by `v`.
88+
//
89+
// This function utilizes the json-iterator library to unmarshal JSON data
90+
// from a string into the specified Go value `v`. If the unmarshalling is
91+
// successful, it populates the value `v`. If an error occurs, it returns the error.
92+
//
93+
// Parameters:
94+
// - `str`: A string containing JSON data to be unmarshalled.
95+
// - `v`: A pointer to the Go value where the unmarshalled data will be stored.
96+
//
97+
// Returns:
98+
// - An error if the unmarshalling fails.
99+
//
100+
// Example:
101+
// err := UnmarshalFromString(jsonString, &myStruct)
102+
func UnmarshalFromString(str string, v interface{}) error {
103+
return _json.UnmarshalFromString(str, v)
104+
}
105+
106+
// Json converts a Go value to its JSON string representation or returns the value directly if it is already a string.
107+
//
108+
// This function checks if the input data is a string; if so, it returns it directly.
109+
// Otherwise, it marshals the input value `data` into a JSON string using the
110+
// MarshalToString function. If an error occurs during marshalling, it returns an empty string.
111+
//
112+
// Parameters:
113+
// - `data`: The Go value to be converted to JSON, or a string to be returned directly.
114+
//
115+
// Returns:
116+
// - A string containing the JSON representation of the input value, or an empty string if an error occurs.
117+
//
118+
// Example:
119+
// jsonStr := Json(myStruct)
120+
func Json(data interface{}) string {
121+
s, ok := data.(string)
122+
if ok {
123+
return s
124+
}
125+
result, err := MarshalToString(data)
126+
if err != nil {
127+
return ""
128+
}
129+
return string(result)
130+
}
131+
132+
// JsonPretty converts a Go value to its pretty-printed JSON string representation or returns the value directly if it is already a string.
133+
//
134+
// This function checks if the input data is a string; if so, it returns it directly.
135+
// Otherwise, it marshals the input value `data` into a formatted JSON string using
136+
// the MarshalIndent function. If an error occurs during marshalling, it returns an empty string.
137+
//
138+
// Parameters:
139+
// - `data`: The Go value to be converted to pretty-printed JSON, or a string to be returned directly.
140+
//
141+
// Returns:
142+
// - A string containing the pretty-printed JSON representation of the input value, or an empty string if an error occurs.
143+
//
144+
// Example:
145+
// jsonPrettyStr := JsonPretty(myStruct)
146+
func JsonPretty(data interface{}) string {
147+
s, ok := data.(string)
148+
if ok {
149+
return s
150+
}
151+
result, err := MarshalIndent(data, "", " ")
152+
if err != nil {
153+
return ""
154+
}
155+
return string(result)
156+
}

0 commit comments

Comments
 (0)