Compare commits
2 Commits
0144f175f0
...
v4.1.12
Author | SHA1 | Date | |
---|---|---|---|
88a5875cfb | |||
03ee33040c |
@@ -3,8 +3,6 @@ package codec
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -68,10 +66,10 @@ func (m *RawMessage) MarshalYAML() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalYAML sets *m to a copy of data.
|
// UnmarshalYAML sets *m to a copy of data.
|
||||||
func (m *RawMessage) UnmarshalYAML(n *yaml.Node) error {
|
func (m *RawMessage) UnmarshalYAML(data []byte) error {
|
||||||
if m == nil {
|
if m == nil {
|
||||||
return errors.New("RawMessage UnmarshalYAML on nil pointer")
|
return errors.New("RawMessage UnmarshalYAML on nil pointer")
|
||||||
}
|
}
|
||||||
*m = append((*m)[0:0], []byte(n.Value)...)
|
*m = append((*m)[0:0], data...)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,5 @@
|
|||||||
package codec
|
package codec
|
||||||
|
|
||||||
import "gopkg.in/yaml.v3"
|
|
||||||
|
|
||||||
// Frame gives us the ability to define raw data to send over the pipes
|
// Frame gives us the ability to define raw data to send over the pipes
|
||||||
type Frame struct {
|
type Frame struct {
|
||||||
Data []byte
|
Data []byte
|
||||||
@@ -28,8 +26,8 @@ func (m *Frame) MarshalYAML() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalYAML set frame data
|
// UnmarshalYAML set frame data
|
||||||
func (m *Frame) UnmarshalYAML(n *yaml.Node) error {
|
func (m *Frame) UnmarshalYAML(data []byte) error {
|
||||||
m.Data = []byte(n.Value)
|
m.Data = append((m.Data)[0:0], data...)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
go.mod
4
go.mod
@@ -6,7 +6,7 @@ require (
|
|||||||
dario.cat/mergo v1.0.1
|
dario.cat/mergo v1.0.1
|
||||||
github.com/DATA-DOG/go-sqlmock v1.5.2
|
github.com/DATA-DOG/go-sqlmock v1.5.2
|
||||||
github.com/KimMachineGun/automemlimit v0.7.0
|
github.com/KimMachineGun/automemlimit v0.7.0
|
||||||
github.com/ash3in/uuidv8 v1.2.0
|
github.com/goccy/go-yaml v1.17.1
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/matoous/go-nanoid v1.5.1
|
github.com/matoous/go-nanoid v1.5.1
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
@@ -19,7 +19,6 @@ require (
|
|||||||
golang.org/x/sync v0.10.0
|
golang.org/x/sync v0.10.0
|
||||||
google.golang.org/grpc v1.69.4
|
google.golang.org/grpc v1.69.4
|
||||||
google.golang.org/protobuf v1.36.3
|
google.golang.org/protobuf v1.36.3
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -31,4 +30,5 @@ require (
|
|||||||
golang.org/x/sys v0.29.0 // indirect
|
golang.org/x/sys v0.29.0 // indirect
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484 // indirect
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484 // indirect
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
go.sum
@@ -4,12 +4,12 @@ github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7Oputl
|
|||||||
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
||||||
github.com/KimMachineGun/automemlimit v0.7.0 h1:7G06p/dMSf7G8E6oq+f2uOPuVncFyIlDI/pBWK49u88=
|
github.com/KimMachineGun/automemlimit v0.7.0 h1:7G06p/dMSf7G8E6oq+f2uOPuVncFyIlDI/pBWK49u88=
|
||||||
github.com/KimMachineGun/automemlimit v0.7.0/go.mod h1:QZxpHaGOQoYvFhv/r4u3U0JTC2ZcOwbSr11UZF46UBM=
|
github.com/KimMachineGun/automemlimit v0.7.0/go.mod h1:QZxpHaGOQoYvFhv/r4u3U0JTC2ZcOwbSr11UZF46UBM=
|
||||||
github.com/ash3in/uuidv8 v1.2.0 h1:2oogGdtCPwaVtyvPPGin4TfZLtOGE5F+W++E880G6SI=
|
|
||||||
github.com/ash3in/uuidv8 v1.2.0/go.mod h1:BnU0wJBxnzdEKmVg4xckBkD+VZuecTFTUP3M0dWgyY4=
|
|
||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||||
|
github.com/goccy/go-yaml v1.17.1 h1:LI34wktB2xEE3ONG/2Ar54+/HJVBriAGJ55PHls4YuY=
|
||||||
|
github.com/goccy/go-yaml v1.17.1/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
|
@@ -2,12 +2,8 @@ package id
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
uuidv8 "github.com/ash3in/uuidv8"
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
nanoid "github.com/matoous/go-nanoid"
|
nanoid "github.com/matoous/go-nanoid"
|
||||||
)
|
)
|
||||||
@@ -25,6 +21,7 @@ type Type int
|
|||||||
const (
|
const (
|
||||||
TypeUnspecified Type = iota
|
TypeUnspecified Type = iota
|
||||||
TypeNanoid
|
TypeNanoid
|
||||||
|
TypeUUIDv7
|
||||||
TypeUUIDv8
|
TypeUUIDv8
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -58,14 +55,14 @@ func (g *Generator) New() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return nanoid.Generate(g.opts.NanoidAlphabet, g.opts.NanoidSize)
|
return nanoid.Generate(g.opts.NanoidAlphabet, g.opts.NanoidSize)
|
||||||
case TypeUUIDv8:
|
case TypeUUIDv7:
|
||||||
timestamp := uint64(time.Now().UnixNano())
|
uid, err := uuid.NewV7()
|
||||||
clockSeq := make([]byte, 2)
|
if err != nil {
|
||||||
if _, err := rand.Read(clockSeq); err != nil {
|
return "", err
|
||||||
return "", fmt.Errorf("failed to generate random clock sequence: %w", err)
|
|
||||||
}
|
}
|
||||||
clockSeqValue := binary.BigEndian.Uint16(clockSeq) & 0x0FFF // Mask to 12 bits
|
return uid.String(), nil
|
||||||
return uuidv8.NewWithParams(timestamp, clockSeqValue, g.opts.UUIDNode[:], uuidv8.TimestampBits48)
|
case TypeUUIDv8:
|
||||||
|
return "", errors.New("unsupported uuid version v8")
|
||||||
}
|
}
|
||||||
return "", errors.New("invalid option, Type unspecified")
|
return "", errors.New("invalid option, Type unspecified")
|
||||||
}
|
}
|
||||||
@@ -82,16 +79,15 @@ func New(opts ...Option) (string, error) {
|
|||||||
if options.NanoidSize <= 0 {
|
if options.NanoidSize <= 0 {
|
||||||
return "", errors.New("invalid option, NanoidSize must be positive integer")
|
return "", errors.New("invalid option, NanoidSize must be positive integer")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nanoid.Generate(options.NanoidAlphabet, options.NanoidSize)
|
return nanoid.Generate(options.NanoidAlphabet, options.NanoidSize)
|
||||||
case TypeUUIDv8:
|
case TypeUUIDv7:
|
||||||
timestamp := uint64(time.Now().UnixNano())
|
uid, err := uuid.NewV7()
|
||||||
clockSeq := make([]byte, 2)
|
if err != nil {
|
||||||
if _, err := rand.Read(clockSeq); err != nil {
|
return "", err
|
||||||
return "", fmt.Errorf("failed to generate random clock sequence: %w", err)
|
|
||||||
}
|
}
|
||||||
clockSeqValue := binary.BigEndian.Uint16(clockSeq) & 0x0FFF // Mask to 12 bits
|
return uid.String(), nil
|
||||||
return uuidv8.NewWithParams(timestamp, clockSeqValue, options.UUIDNode[:], uuidv8.TimestampBits48)
|
case TypeUUIDv8:
|
||||||
|
return "", errors.New("unsupported uuid version v8")
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", errors.New("invalid option, Type unspecified")
|
return "", errors.New("invalid option, Type unspecified")
|
||||||
@@ -145,7 +141,7 @@ func WithUUIDNode(node [6]byte) Option {
|
|||||||
// NewOptions returns new Options struct filled by opts
|
// NewOptions returns new Options struct filled by opts
|
||||||
func NewOptions(opts ...Option) Options {
|
func NewOptions(opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
Type: TypeUUIDv8,
|
Type: TypeUUIDv7,
|
||||||
NanoidAlphabet: DefaultNanoidAlphabet,
|
NanoidAlphabet: DefaultNanoidAlphabet,
|
||||||
NanoidSize: DefaultNanoidSize,
|
NanoidSize: DefaultNanoidSize,
|
||||||
UUIDNode: generatedNode,
|
UUIDNode: generatedNode,
|
||||||
|
@@ -6,7 +6,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"github.com/goccy/go-yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Duration int64
|
type Duration int64
|
||||||
@@ -58,9 +58,9 @@ func (d Duration) MarshalYAML() (interface{}, error) {
|
|||||||
return time.Duration(d).String(), nil
|
return time.Duration(d).String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Duration) UnmarshalYAML(n *yaml.Node) error {
|
func (d *Duration) UnmarshalYAML(data []byte) error {
|
||||||
var v interface{}
|
var v interface{}
|
||||||
if err := yaml.Unmarshal([]byte(n.Value), &v); err != nil {
|
if err := yaml.Unmarshal(data, &v); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
switch value := v.(type) {
|
switch value := v.(type) {
|
||||||
|
@@ -6,7 +6,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"github.com/goccy/go-yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMarshalYAML(t *testing.T) {
|
func TestMarshalYAML(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user