| 1 | // Copyright (C) 2013-2018 by Maxim Bublis <[email protected]> |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining |
| 4 | // a copy of this software and associated documentation files (the |
| 5 | // "Software"), to deal in the Software without restriction, including |
| 6 | // without limitation the rights to use, copy, modify, merge, publish, |
| 7 | // distribute, sublicense, and/or sell copies of the Software, and to |
| 8 | // permit persons to whom the Software is furnished to do so, subject to |
| 9 | // the following conditions: |
| 10 | // |
| 11 | // The above copyright notice and this permission notice shall be |
| 12 | // included in all copies or substantial portions of the Software. |
| 13 | // |
| 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 18 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 19 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 20 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | package uuidscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/ |
| 23 | |
| 24 | import ( |
| 25 | "database/sqlscip-go gomod github.com/golang/go/src go1.25 `database/sql`/" |
| 26 | "database/sql/driverscip-go gomod github.com/golang/go/src go1.25 `database/sql/driver`/" |
| 27 | "fmtscip-go gomod github.com/golang/go/src go1.25 fmt/" |
| 28 | ) |
| 29 | |
| 30 | var _ driverscip-go gomod github.com/golang/go/src go1.25 `database/sql/driver`/.Valuerscip-go gomod github.com/golang/go/src go1.25 `database/sql/driver`/Valuer# = UUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID#{} |
| 31 | var _ sqlscip-go gomod github.com/golang/go/src go1.25 `database/sql`/.Scannerscip-go gomod github.com/golang/go/src go1.25 `database/sql`/Scanner# = (*UUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID#)(nil) |
| 32 | |
| 33 | // Value implements the driver.Valuer interface. |
| 34 | func (u UUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID#) Valuescip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID#Value().() (driverscip-go gomod github.com/golang/go/src go1.25 `database/sql/driver`/.Valuescip-go gomod github.com/golang/go/src go1.25 `database/sql/driver`/Value#, error) { |
| 35 | return u.Stringscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID#String().(), nil |
| 36 | } |
| 37 | |
| 38 | // Scan implements the sql.Scanner interface. |
| 39 | // A 16-byte slice will be handled by UnmarshalBinary, while |
| 40 | // a longer byte slice or a string will be handled by UnmarshalText. |
| 41 | func (u *UUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID#) Scanscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID#Scan().(src any) error { |
| 42 | switch src := src.(type) { |
| 43 | case UUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID#: // support gorm convert from UUID to NullUUID |
| 44 | *u = src |
| 45 | return nil |
| 46 | |
| 47 | case []byte: |
| 48 | if len(src) == Sizescip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/Size. { |
| 49 | return u.UnmarshalBinaryscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID#UnmarshalBinary().(src) |
| 50 | } |
| 51 | return u.UnmarshalTextscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID#UnmarshalText().(src) |
| 52 | |
| 53 | case string: |
| 54 | uu, err := FromStringscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/FromString().(src) |
| 55 | *u = uu |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("%w %T to UUID", ErrTypeConvertErrorscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/ErrTypeConvertError., src) |
| 60 | } |
| 61 | |
| 62 | // NullUUID can be used with the standard sql package to represent a |
| 63 | // UUID value that can be NULL in the database. |
| 64 | type NullUUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID# struct { |
| 65 | UUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#UUID. UUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID# |
| 66 | Validscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#Valid. bool |
| 67 | } |
| 68 | |
| 69 | // Value implements the driver.Valuer interface. |
| 70 | func (u NullUUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#) Valuescip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#Value().() (driverscip-go gomod github.com/golang/go/src go1.25 `database/sql/driver`/.Valuescip-go gomod github.com/golang/go/src go1.25 `database/sql/driver`/Value#, error) { |
| 71 | if !u.Validscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#Valid. { |
| 72 | return nil, nil |
| 73 | } |
| 74 | // Delegate to UUID Value function |
| 75 | return u.UUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#UUID..Valuescip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID#Value().() |
| 76 | } |
| 77 | |
| 78 | // Scan implements the sql.Scanner interface. |
| 79 | func (u *NullUUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#) Scanscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#Scan().(src any) error { |
| 80 | if src == nil { |
| 81 | u.UUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#UUID., u.Validscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#Valid. = Nilscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/Nil., false |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | // Delegate to UUID Scan function |
| 86 | u.Validscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#Valid. = true |
| 87 | return u.UUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#UUID..Scanscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID#Scan().(src) |
| 88 | } |
| 89 | |
| 90 | var nullJSONscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/nullJSON. = []byte("null") |
| 91 | |
| 92 | // MarshalJSON marshals the NullUUID as null or the nested UUID |
| 93 | func (u NullUUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#) MarshalJSONscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#MarshalJSON().() ([]byte, error) { |
| 94 | if !u.Validscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#Valid. { |
| 95 | return nullJSONscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/nullJSON., nil |
| 96 | } |
| 97 | var buf [38]byte |
| 98 | buf[0] = '"' |
| 99 | encodeCanonicalscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/encodeCanonical().(buf[1:37], u.UUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#UUID.) |
| 100 | buf[37] = '"' |
| 101 | return buf[:], nil |
| 102 | } |
| 103 | |
| 104 | // UnmarshalJSON unmarshals a NullUUID |
| 105 | func (u *NullUUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#) UnmarshalJSONscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#UnmarshalJSON().(b []byte) error { |
| 106 | if string(b) == "null" { |
| 107 | u.UUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#UUID., u.Validscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#Valid. = Nilscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/Nil., false |
| 108 | return nil |
| 109 | } |
| 110 | if n := len(b); n >= 2 && b[0] == '"' { |
| 111 | b = b[1 : n-1] |
| 112 | } |
| 113 | err := u.UUIDscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#UUID..UnmarshalTextscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/UUID#UnmarshalText().(b) |
| 114 | u.Validscip-go gomod github.com/gofrs/uuid/v5 . `github.com/gofrs/uuid/v5`/NullUUID#Valid. = (err == nil) |
| 115 | return err |
| 116 | } |