Cleanup go.mod, document things better
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details

This commit is contained in:
Tyler 2023-02-04 20:31:09 -05:00
parent 37776d4257
commit 28a5d131a6
8 changed files with 41 additions and 31 deletions

View File

@ -8,8 +8,8 @@ import (
)
type Options struct {
Encoder encoder.Encoder `query:"encoder" default:"msgpack"`
Size int `query:"size" default:"128"`
Encoder encoder.Encoder `default:"msgpack"`
Size int `default:"128"`
}
type Cache struct {

View File

@ -7,7 +7,7 @@ import (
)
type Options struct {
Encoder encoder.Encoder `query:"encoder" default:"msgpack"`
Encoder encoder.Encoder `default:"msgpack"`
Servers []string `default:"127.0.0.1:11211"`
}

View File

@ -13,8 +13,8 @@ var (
)
type Options struct {
Encoder encoder.Encoder `query:"encoder" default:"msgpack"`
DefaultExpiration time.Duration `query:"defaultExpiration"`
Encoder encoder.Encoder `default:"msgpack"`
DefaultExpiration time.Duration `query:"defaultExpiration" default:"1m"`
CleanupTime time.Duration `query:"cleanupTime" default:"5m"`
}
@ -24,7 +24,7 @@ type Cache struct {
}
func New(options Options) (*Cache, error) {
c := cache.New(1*time.Minute, options.CleanupTime)
c := cache.New(options.DefaultExpiration, options.CleanupTime)
return &Cache{
options: options,

View File

@ -7,10 +7,10 @@ import (
)
type Options struct {
Encoder encoder.Encoder `query:"encoder" default:"msgpack"`
Encoder encoder.Encoder `default:"msgpack"`
Address string `default:"127.0.0.1"`
DB int `default:"0" query:"db"`
Password string `query:"password"`
DB int `default:"0"`
Password string
}
type Cache struct {

View File

@ -1,5 +1,7 @@
package encoder
// EncodeValue is a helper that handles conversion of values
// byte slices and strings are converted directly, everything else is marshalled
func EncodeValue(encoder Encoder, val any) ([]byte, error) {
var v []byte
@ -14,6 +16,8 @@ func EncodeValue(encoder Encoder, val any) ([]byte, error) {
return v, nil
}
// DecodeValue is a helper that handles decoding of values
// byte slices and strings can be assigned directly
func DecodeValue(encoder Encoder, b []byte, v any) error {
switch v := v.(type) {
case *[]byte:

View File

@ -10,23 +10,29 @@ var (
XML xmlEncoder
JSON jsonEncoder
MsgPack msgPackEncoder
encoders = map[string]Encoder{
"json": JSON,
"xml": XML,
"msgpack": MsgPack,
}
)
func EncoderFrom(name string) Encoder {
var enc Encoder
switch name {
case "json":
enc = JSON
case "xml":
enc = XML
default: // Default is also msgpack
enc = MsgPack
}
return enc
// Register allows you to register your own encoders
func Register(name string, enc Encoder) {
encoders[name] = enc
}
// From will return an encoder for the specified key
func From(name string) Encoder {
if enc, ok := encoders[name]; ok {
return enc
}
return MsgPack
}
// Encoder is the base interface for encoding/decoding values
type Encoder interface {
Unmarshal(b []byte, dest any) error
Marshal(value any) ([]byte, error)

13
go.mod
View File

@ -6,6 +6,9 @@ require (
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/hashicorp/golang-lru v0.5.3
github.com/hoisie/redis v0.0.0-20160730154456-b5c6e81454e0
github.com/iancoleman/strcase v0.2.0
github.com/onsi/ginkgo/v2 v2.8.0
github.com/onsi/gomega v1.26.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/vmihailenco/msgpack/v4 v4.2.0
)
@ -14,19 +17,13 @@ require (
github.com/go-logr/logr v1.2.3 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/vmihailenco/tagparser v0.1.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
google.golang.org/appengine v1.6.1 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
require (
github.com/iancoleman/strcase v0.2.0
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/onsi/ginkgo/v2 v2.8.0
github.com/onsi/gomega v1.26.0
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)

View File

@ -11,6 +11,9 @@ import (
"time"
)
// decodeQuery is a helper that behaves like gorilla/schema,
// encoding/json, etc. It takes in a url.Values and decodes it into the
// dest struct - assigning defaults if nothing is populated.
func decodeQuery(query url.Values, dest any) error {
destType := reflect.TypeOf(dest)
@ -76,7 +79,7 @@ var (
func decodeType(t reflect.Type, val []string, isDefault bool) (any, error) {
switch t {
case encoderType:
return encoder.EncoderFrom(val[0]), nil
return encoder.From(val[0]), nil
case durationType:
return time.ParseDuration(val[0])
}