Skip to content

Commit

Permalink
add MustNew* method to JSON value type
Browse files Browse the repository at this point in the history
  • Loading branch information
demdxx committed Dec 10, 2023
1 parent 2cae4a7 commit 8c29539
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.20.x
go-version: 1.21.x
- name: Checkout code
uses: actions/checkout@v3
- name: Run linters
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
strategy:
fail-fast: false
matrix:
go-version: [1.20.x, 1.21.x]
go-version: [1.21.x]
steps:
- uses: actions/setup-go@v3
with:
Expand Down
26 changes: 15 additions & 11 deletions .vscode/targets.log
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ make all --print-data-base --no-builtin-variables --no-builtin-rules --question
make: *** No rule to make target `all'. Stop.


# Make data base, printed on Wed Nov 1 11:53:41 2023
# Make data base, printed on Sun Dec 10 17:47:51 2023

# Variables

Expand All @@ -22,7 +22,6 @@ make: *** No rule to make target `all'. Stop.
VSCODE_CRASH_REPORTER_PROCESS_TYPE = extensionHost
# environment
NVM_INC = /Users/dem/.nvm/versions/node/v16.20.1/include/node

# automatic
?D = $(patsubst %/,%,$(dir $?))
# automatic
Expand All @@ -37,6 +36,7 @@ SHELL = /bin/sh
VSCODE_NLS_CONFIG = {"locale":"en-gb","osLocale":"en-es","availableLanguages":{},"_languagePackSupport":true}
# environment
_ = /usr/bin/make

# makefile (from `Makefile', line 1)
MAKEFILE_LIST := Makefile
# environment
Expand All @@ -56,11 +56,13 @@ GOPATH = /Users/dem/go
# environment
NVM_BIN = /Users/dem/.nvm/versions/node/v16.20.1/bin
# environment
LaunchInstanceID = 1C567B53-48E5-4227-B97F-1ED2F0C55777
# environment
ELECTRON_RUN_AS_NODE = 1
# default
.FEATURES := target-specific order-only second-expansion else-if archives jobserver check-symlink
# environment
SSH_AUTH_SOCK = /private/tmp/com.apple.launchd.r3ReU95XXc/Listeners
SSH_AUTH_SOCK = /private/tmp/com.apple.launchd.NMIOoGLGXn/Listeners
# automatic
%F = $(notdir $%)
# environment
Expand All @@ -78,7 +80,7 @@ HOME = /Users/dem
# default
MAKEFILEPATH = $(shell /usr/bin/xcode-select -print-path 2>/dev/null || echo /Developer)/Makefiles
# environment
VSCODE_CODE_CACHE_PATH = /Users/dem/Library/Application Support/Code/CachedData/f1b07bd25dfad64b0167beb15359ae573aecd2cc
VSCODE_CODE_CACHE_PATH = /Users/dem/Library/Application Support/Code/CachedData/1a5daa3a0231a0fbba4f14db7ec463cf99d7768e
# environment
LOGNAME = dem
# environment
Expand Down Expand Up @@ -114,7 +116,7 @@ TMPDIR = /var/folders/15/sxnh_hg94zb4xz4fp69skvs80000gn/T/
# automatic
*F = $(notdir $*)
# environment
VSCODE_IPC_HOOK = /Users/dem/Library/Application Support/Code/1.83-main.sock
VSCODE_IPC_HOOK = /Users/dem/Library/Application Support/Code/1.84-main.sock
# environment
MallocNanoZone = 0
# makefile
Expand All @@ -126,7 +128,7 @@ MFLAGS = -Rrqp
# environment
NVM_DIR = /Users/dem/.nvm
# environment
XPC_SERVICE_NAME = application.com.microsoft.VSCode.26910363.26910369
XPC_SERVICE_NAME = application.com.microsoft.VSCode.36957161.36957167
# environment
HOMEBREW_PREFIX = /opt/homebrew
# automatic
Expand All @@ -145,6 +147,8 @@ MAKEFILES :=
<F = $(notdir $<)
# environment
LC_ALL = C
# environment
SECURITYSESSIONID = 186ab
# automatic
^F = $(notdir $^)
# default
Expand All @@ -154,17 +158,17 @@ MAKELEVEL := 0
# environment
LANG = C
# environment
VSCODE_PID = 1363
VSCODE_PID = 1342
# variable set hash-table stats:
# Load=70/1024=7%, Rehash=0, Collisions=2/94=2%
# Load=72/1024=7%, Rehash=0, Collisions=2/96=2%

# Pattern-specific Variable Values

# No pattern-specific variable values.

# Directories

# . (device 16777232, inode 3209637): 27 files, no impossibilities.
# . (device 16777231, inode 3209637): 27 files, no impossibilities.

# 27 files, no impossibilities in 1 directories.

Expand All @@ -181,7 +185,6 @@ all:
# File does not exist.
# File has not been updated.
# variable set hash-table stats:

# Load=0/32=0%, Rehash=0, Collisions=0/0=0%

.PHONY: fmt test tidy bench lint help
Expand Down Expand Up @@ -226,6 +229,7 @@ help:
# Implicit rule search has not been done.
# File does not exist.
# File has not been updated.

# commands to execute (from `Makefile', line 27):
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' Makefile | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

Expand Down Expand Up @@ -286,6 +290,6 @@ lint:
# strcache size: total = 4096 / max = 4096 / min = 4096 / avg = 4096
# strcache free: total = 4087 / max = 4087 / min = 4087 / avg = 4087

# Finished Make data base on Wed Nov 1 11:53:41 2023
# Finished Make data base on Sun Dec 10 17:47:51 2023


2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb h1:c0vyKkb6yr3KR7jEfJaOSv4lG7xPkbN6r52aJz1d8a8=
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 h1:FVCohIoYO7IJoDDVpV2pdq7SgrMH6wHnuTyrdrxJNoY=
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0/go.mod h1:OdE7CF6DbADk7lN8LIKRzRJTTZXIjtWgA5THM5lhBAw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
9 changes: 9 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ func NewJSON[T any](val any) (*JSON[T], error) {
return &obj, nil
}

// MustJSON creates new JSON object
func MustJSON[T any](val any) *JSON[T] {
obj, err := NewJSON[T](val)
if err != nil {
panic(err)
}
return obj
}

// String value
func (f *JSON[T]) String() string {
if f == nil {
Expand Down
4 changes: 4 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ func TestJSON(t *testing.T) {
assert.Equal(t, "1", js.Data["number"])

assert.Error(t, js.SetValue(map[string]any{"invalid": 1}))

assert.Panics(t, func() {
MustJSON[map[string]string](nil)
})
}
9 changes: 9 additions & 0 deletions nullable_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ func NewNullableJSON[T any](val any) (*NullableJSON[T], error) {
return &obj, nil
}

// MustNullableJSON creates new JSON object
func MustNullableJSON[T any](val any) *NullableJSON[T] {
obj, err := NewNullableJSON[T](val)
if err != nil {
panic(err)
}
return obj
}

// String value
func (f *NullableJSON[T]) String() string {
if f == nil || f.Data == nil {
Expand Down
25 changes: 25 additions & 0 deletions nullable_json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gosql

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNullableJSON(t *testing.T) {
js, err := NewNullableJSON[map[string]string](map[string]any{
"name": "Yoda",
"number": "1",
})
if !assert.NoError(t, err) {
return
}
assert.Equal(t, "Yoda", (*js.Data)["name"])
assert.Equal(t, "1", (*js.Data)["number"])

assert.Error(t, js.SetValue(map[string]any{"invalid": 1}))

assert.Panics(t, func() {
MustNullableJSON[map[string]string]([]int{1, 2, 3})
})
}

0 comments on commit 8c29539

Please sign in to comment.