Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: manicar2093/goption
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.8.0
Choose a base ref
...
head repository: manicar2093/goption
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref

Commits on Jun 12, 2024

  1. Copy the full SHA
    90adc1b View commit details
  2. Copy the full SHA
    603d29d View commit details
  3. chore: change var name

    manicar2093 committed Jun 12, 2024
    Copy the full SHA
    dd79489 View commit details
  4. Merge pull request #13 from manicar2093/fix-null-unmarshal

    fix: null unmarshal
    manicar2093 authored Jun 12, 2024
    Copy the full SHA
    2f59843 View commit details

Commits on Jun 13, 2024

  1. Copy the full SHA
    2b3fdef View commit details
  2. Copy the full SHA
    a2b4ef1 View commit details
  3. Copy the full SHA
    4962eee View commit details
  4. Merge pull request #14 from manicar2093/fix/support-custom-types-on-u…

    …nmarshal
    
    fix: add support custom types on unmarshal
    manicar2093 authored Jun 13, 2024
    Copy the full SHA
    91d8ba5 View commit details
  5. Copy the full SHA
    3c04257 View commit details

Commits on Jul 17, 2024

  1. Copy the full SHA
    60a7239 View commit details
  2. Merge pull request #15 from manicar2093/fix/multiline-json-parsing

    fix: add handling multiline's strings to be unmarshal
    manicar2093 authored Jul 17, 2024
    Copy the full SHA
    6f23076 View commit details
  3. Copy the full SHA
    83091d8 View commit details
  4. Copy the full SHA
    6519b6a View commit details

Commits on Jul 18, 2024

  1. Merge pull request #16 from manicar2093/fix/add-carriage-return-to-mu…

    …ltiline-strings
    
    fix: add handling multiline's strings \r carriage return
    manicar2093 authored Jul 18, 2024
    Copy the full SHA
    1f1a0c8 View commit details
  2. Copy the full SHA
    1420626 View commit details

Commits on Jul 19, 2024

  1. Copy the full SHA
    97e57f6 View commit details
  2. Merge pull request #17 from manicar2093/fix/suppor-other-types

    fix: add handling for numbers and floats
    manicar2093 authored Jul 19, 2024
    Copy the full SHA
    1a8d037 View commit details
  3. Copy the full SHA
    94a69c9 View commit details

Commits on Oct 18, 2024

  1. Copy the full SHA
    cfde411 View commit details
  2. Copy the full SHA
    6b6515c View commit details
  3. Copy the full SHA
    fd58825 View commit details
  4. Copy the full SHA
    4ef8d72 View commit details
  5. Merge pull request #19 from manicar2093/fix/unmarshal-text

    fix: unmarshal text
    manicar2093 authored Oct 18, 2024
    Copy the full SHA
    a44d4ca View commit details
  6. Copy the full SHA
    b984888 View commit details

Commits on Nov 6, 2024

  1. Copy the full SHA
    2e6d061 View commit details
  2. Copy the full SHA
    ea29564 View commit details
  3. Merge pull request #20 from manicar2093/fix/check-type-implement-sql-…

    …scann
    
    fix: check type implement sql scan
    manicar2093 authored Nov 6, 2024
    Copy the full SHA
    7b752ed View commit details
  4. Copy the full SHA
    c9edf67 View commit details

Commits on Dec 10, 2024

  1. Copy the full SHA
    4966642 View commit details
  2. Merge pull request #21 from manicar2093/fix/unmarshal-text-with-uuid

    fix: change regrex to identify uuid
    manicar2093 authored Dec 10, 2024
    Copy the full SHA
    e98a7df View commit details
  3. Copy the full SHA
    9b38034 View commit details

Commits on Jan 9, 2025

  1. fix: add slice support

    manicar2093 committed Jan 9, 2025
    Copy the full SHA
    7821ed7 View commit details
  2. Merge pull request #22 from manicar2093/fix/support-slices

    fix: add slice support
    manicar2093 authored Jan 9, 2025
    Copy the full SHA
    b114827 View commit details
  3. Copy the full SHA
    b0dea97 View commit details
Showing with 419 additions and 79 deletions.
  1. +1 −1 .cz.toml
  2. +1 −1 .github/workflows/bump_version.yml
  3. +1 −0 .gitignore
  4. +60 −0 CHANGELOG.md
  5. +0 −10 echo.go
  6. +13 −10 go.mod
  7. +24 −30 go.sum
  8. +63 −11 json.go
  9. +62 −10 json_test.go
  10. +27 −6 sql.go
  11. +35 −0 sql_test.go
  12. +21 −0 text.go
  13. +111 −0 text_test.go
2 changes: 1 addition & 1 deletion .cz.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.commitizen]
name = "cz_conventional_commits"
version = "0.8.0"
version = "0.9.3"
tag_format = "v$version"
bump_message = "bump: Semantic Release Bot: Realease Version: $new_version 🤖🚀 [skip ci]"
version_files = [
2 changes: 1 addition & 1 deletion .github/workflows/bump_version.yml
Original file line number Diff line number Diff line change
@@ -26,6 +26,6 @@ jobs:
with:
distribution: goreleaser
version: latest
args: release --rm-dist
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

dist/
.idea
60 changes: 60 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,63 @@
## v0.9.3 (2025-01-09)

### Fix

- add slice support

## v0.9.2 (2024-12-10)

### Fix

- change regrex to identify uuid

## v0.9.1 (2024-11-06)

### Fix

- add sql valuer implementation
- add call of scan method if implemented

## v0.9.0 (2024-10-18)

### Feat

- add new way to identify strings and numbers for unmarshalText
- add unmarshalText with tests

### Fix

- change unmarshalText implementation

## v0.8.5 (2024-07-19)

### Fix

- add handling for numbers and floats

## v0.8.4 (2024-07-18)

### Fix

- add handling multiline's strings \r carriage return

## v0.8.3 (2024-07-17)

### Fix

- add handling multiline's strings to be unmarshal

## v0.8.2 (2024-06-13)

### Fix

- add support for custom types

## v0.8.1 (2024-06-12)

### Fix

- add native null to be handle in a proper way

## v0.8.0 (2024-05-24)

### Feat
10 changes: 0 additions & 10 deletions echo.go

This file was deleted.

23 changes: 13 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
module github.com/manicar2093/goption

go 1.21
go 1.22

toolchain go1.22.8

require (
github.com/mattn/go-sqlite3 v1.14.16
github.com/onsi/ginkgo/v2 v2.17.1
github.com/onsi/gomega v1.30.0
github.com/onsi/ginkgo/v2 v2.20.2
github.com/onsi/gomega v1.34.1
)

require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.17.0 // indirect
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/tools v0.24.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
54 changes: 24 additions & 30 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,42 +1,36 @@
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 h1:5iH8iuqE5apketRbSFBy+X1V0o+l+8NF1avt4HWl7cA=
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/onsi/ginkgo/v2 v2.17.1 h1:V++EzdbhI4ZV4ev0UTIj0PzhzOcReJFyJaLjtSF55M8=
github.com/onsi/ginkgo/v2 v2.17.1/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/onsi/ginkgo/v2 v2.20.2 h1:7NVCeyIWROIAheY21RLS+3j2bb52W0W82tkberYytp4=
github.com/onsi/ginkgo/v2 v2.20.2/go.mod h1:K9gyxPIlb+aIvnZ8bd9Ak+YP18w3APlR+5coaZoE2ag=
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
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.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
74 changes: 63 additions & 11 deletions json.go
Original file line number Diff line number Diff line change
@@ -2,23 +2,21 @@ package goption

import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
)

func (c *Optional[T]) UnmarshalJSON(data []byte) error {
unquoted, err := strconv.Unquote(string(data))
if err != nil {
return err
}
if unquoted == "null" {
unquoted = ""
}
c.isValidValue = getIsValidDataBool(unquoted)
if err := json.Unmarshal(data, &c.value); err != nil {
return err
var (
asString = string(data)
)
if strings.HasPrefix(asString, "\"") {
return c.stringUnmarshall(asString)
}
return nil

return c.numberUnmarshal(asString)
}

func (c Optional[T]) MarshalJSON() ([]byte, error) {
@@ -27,3 +25,57 @@ func (c Optional[T]) MarshalJSON() ([]byte, error) {
}
return json.Marshal(c.value)
}

func isNull(data string) bool {
return data == "null"
}

func (c *Optional[T]) stringUnmarshall(data string) error {
var (
valuer struct {
Value string `json:"value"`
}
cleanables = []struct {
find string
replacement string
}{
{find: "\n", replacement: "\\n"},
{find: "\r", replacement: "\\r"},
}
asJsonString = fmt.Sprintf(`{"value": %s}`, data)
)
for _, item := range cleanables {
asJsonString = strings.ReplaceAll(asJsonString, item.find, item.replacement)
}

if err := json.Unmarshal([]byte(asJsonString), &valuer); err != nil {
return err
}

if isNull(valuer.Value) {
valuer.Value = ""
}

c.isValidValue = getIsValidDataBool(valuer.Value)

return c.unmarshallIntoValueIfValid([]byte(strconv.Quote(valuer.Value)))
}

func (c *Optional[T]) numberUnmarshal(data string) error {
if isNull(data) {
data = ""
}

c.isValidValue = getIsValidDataBool(data)
return c.unmarshallIntoValueIfValid([]byte(data))
}

func (c *Optional[T]) unmarshallIntoValueIfValid(data []byte) error {
if !c.isValidValue {
return nil
}
return json.Unmarshal(
[]byte(data),
&c.value,
)
}
72 changes: 62 additions & 10 deletions json_test.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package goption_test
import (
"encoding/json"
"fmt"
"time"

"github.com/manicar2093/goption"
. "github.com/onsi/ginkgo/v2"
@@ -15,36 +16,87 @@ type ExampleStuct struct {
Age goption.Optional[int] `json:"age"`
}

func asJsonString(v any) string {
return fmt.Sprintf(
`"%v"`,
v,
)
}

var _ = Describe("Json", func() {

Describe("UnmarshalJSON", func() {

It("generates optional from json string", func() {
It("generates optional from json number", func() {
var (
expectedNameData = 10000
expectedNameDataString = "10000"
jsonData = []byte(expectedNameDataString)
holder = goption.Empty[int]()
)
err := holder.UnmarshalJSON(jsonData)

Expect(err).ToNot(HaveOccurred())
Expect(holder.Get()).To(Equal(expectedNameData))
})

It("generates optional from json float", func() {
var (
expectedNameData = "a name"
jsonData = []byte(fmt.Sprintf(
`"%v"`,
expectedNameData,
))
holder = goption.Empty[string]()
expectedNameData = 100.00
expectedNameDataString = "100.00"
jsonData = []byte(expectedNameDataString)
holder = goption.Empty[float64]()
)
err := holder.UnmarshalJSON(jsonData)

Expect(err).ToNot(HaveOccurred())
Expect(holder.Get()).To(Equal(expectedNameData))
})

DescribeTable("generates optional from json string", func(expectedNameData any) {
var (
jsonData = []byte(asJsonString(expectedNameData))
holder = goption.Empty[string]()
)
err := holder.UnmarshalJSON(jsonData)

Expect(err).ToNot(HaveOccurred())
Expect(holder.Get()).To(Equal(expectedNameData))
},
Entry("one line string", "a name"),
Entry("multiline strings", `a name
a name
a name
a name`),
Entry("multiline strings with carriage return", "\r\na name\r\n"),
)

When("is as null", func() {
It("creates an empty optional", func() {
DescribeTable("creates an empty optional", func(jsonData []byte) {
var (
jsonData = []byte(`"null"`)
holder = goption.Empty[string]()
holder = goption.Empty[string]()
)
err := holder.UnmarshalJSON(jsonData)

Expect(err).ToNot(HaveOccurred())
Expect(holder.IsPresent()).To(BeFalse())
},
Entry("null as string", []byte(`"null"`)),
Entry("native null", []byte(`null`)),
)

It("handles custom types", func() {
type testType time.Time
var (
holder = goption.Empty[testType]()
)
err := holder.UnmarshalJSON([]byte(`"null"`))

Expect(err).ToNot(HaveOccurred())
Expect(holder.IsPresent()).To(BeFalse())
})

})

When("is zero", func() {
Loading