Skip to content

Commit

Permalink
feat: init json utils
Browse files Browse the repository at this point in the history
  • Loading branch information
5aaee9 committed Jun 6, 2023
1 parent e8b3db3 commit a3e0fb5
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/go
{
"name": "Go",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/go:0-1.20",
"features": {
"ghcr.io/devcontainers/features/nix:1": {},
"ghcr.io/guiyomh/features/golangci-lint:0": {}
},
"customizations": {
"vscode": {
"extensions": [
"golang.Go"
]
}
}

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "go version",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
20 changes: 20 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Go test

on: [push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Test
run: go test -v ./...

- name: Update coverage report
uses: ncruces/go-coverage-report@v0
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# utils

[![Go Coverage](https://github.com/5aaee9/utils/wiki/coverage.svg)](https://raw.githack.com/wiki/5aaee9/utils/coverage.html)
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/5aaee9/utils

go 1.20

require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
67 changes: 67 additions & 0 deletions pkgs/json/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package json

import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
)

func PettryJSON(input JSON) string {
data, err := json.MarshalIndent(input, "", "\t")
if err != nil {
panic(err)
}

return string(data)
}

func MergeJSON(a, b JSON) JSON {
for k, v := range b {
a[k] = v
}

return a
}

func IntoJSON[T any](input T) (JSON, error) {
var data JSON
b, err := json.Marshal(input)
if err != nil {
return nil, err
}

err = json.Unmarshal(b, &data)
if err != nil {
return nil, err
}

return data, nil
}

// Scan scan value into Jsonb, implements sql.Scanner interface
func (j *JSON) Scan(value interface{}) error {
bytes, ok := value.([]byte)
if !ok {
str, ok := value.(string)
if !ok {
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
}

bytes = []byte(str)
}

result := JSON{}
err := json.Unmarshal(bytes, &result)
*j = JSON(result)
return err
}

// Value return json value, implement driver.Valuer interface
func (j JSON) Value() (driver.Value, error) {
return json.Marshal(j)
}

func (j JSON) GormDataType() string {
return "JSON"
}
33 changes: 33 additions & 0 deletions pkgs/json/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package json_test

import (
"testing"

"github.com/5aaee9/utils/pkgs/json"
"github.com/stretchr/testify/assert"
)

type TestStruct struct {
Data string `json:"test"`
}

func TestIntoJSON(t *testing.T) {
out, err := json.IntoJSON(TestStruct{Data: "test random str"})
assert.NoError(t, err)
assert.Equal(t, "test random str", out["test"])
}

func TestIntoJSONPointer(t *testing.T) {
out, err := json.IntoJSON(&TestStruct{Data: "test random str"})
assert.NoError(t, err)
assert.Equal(t, "test random str", out["test"])
}

func TestMergeJSON(t *testing.T) {
out, err := json.IntoJSON(TestStruct{Data: "test random str"})
assert.NoError(t, err)

out = json.MergeJSON(out, json.JSON{"data": 123})
assert.Equal(t, "test random str", out["test"])
assert.Equal(t, 123, out["data"])
}
4 changes: 4 additions & 0 deletions pkgs/json/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package json


type JSON map[string]interface{}

0 comments on commit a3e0fb5

Please sign in to comment.