From 534cb183f7b7f184a3a758a351b7c2c74a621d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Tue, 24 May 2022 23:17:31 +0800 Subject: [PATCH] - init --- .drone.yml | 17 +++++++++++++++++ .gitignore | 8 ++++++++ go.mod | 3 +++ gojson.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ gojson_test.go | 1 + version.go | 3 +++ version_test.go | 7 +++++++ 7 files changed, 86 insertions(+) create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 gojson.go create mode 100644 gojson_test.go create mode 100644 version.go create mode 100644 version_test.go diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..c56c479 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,17 @@ +kind: pipeline +type: docker +name: clone + +steps: + - name: Test + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -v ./... + - name: Benchmark + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -bench=. -benchmem \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..442794a --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.env +.git +.svn +.idea +.vscode +*.log +gomod.sh +/vendor/ \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..122c32f --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module go.dtapp.net/gojson + +go 1.18 diff --git a/gojson.go b/gojson.go new file mode 100644 index 0000000..a1e046b --- /dev/null +++ b/gojson.go @@ -0,0 +1,47 @@ +package gojson + +import "encoding/json" + +func Encode(v interface{}) (string, error) { + bytes, err := json.Marshal(v) + if err != nil { + return "", err + } + return string(bytes), nil +} + +func MarshalToString(msg interface{}) (string, error) { + j, e := json.Marshal(msg) + if e != nil { + return "", e + } + return string(j), nil +} + +func JsonDecode(data string) (map[string]interface{}, error) { + var dat map[string]interface{} + err := json.Unmarshal([]byte(data), &dat) + return dat, err +} + +func JsonDecodeNoError(data string) map[string]interface{} { + var dat map[string]interface{} + _ = json.Unmarshal([]byte(data), &dat) + return dat +} + +func JsonEncode(data interface{}) (string, error) { + jsons, err := json.Marshal(data) + return string(jsons), err +} + +func JsonEncodeNoError(data interface{}) string { + jsons, _ := json.Marshal(data) + return string(jsons) +} + +func JsonDecodesNoError(data string) []string { + var dat []string + _ = json.Unmarshal([]byte(data), &dat) + return dat +} diff --git a/gojson_test.go b/gojson_test.go new file mode 100644 index 0000000..e3e8d73 --- /dev/null +++ b/gojson_test.go @@ -0,0 +1 @@ +package gojson diff --git a/version.go b/version.go new file mode 100644 index 0000000..ad968b6 --- /dev/null +++ b/version.go @@ -0,0 +1,3 @@ +package gojson + +const Version = "1.0.0" diff --git a/version_test.go b/version_test.go new file mode 100644 index 0000000..06c16c9 --- /dev/null +++ b/version_test.go @@ -0,0 +1,7 @@ +package gojson + +import "testing" + +func TestVersion(t *testing.T) { + t.Log(Version) +}