mirror of
https://github.com/nabbar/golib.git
synced 2025-09-26 20:01:15 +08:00
Package Certificates:
- fix not well working viper hook function Package Semaphore: - Add Clone function in Semaphore interface Package Duration: - Fix overflow capabilitie with negative value to package duration & duration/big
This commit is contained in:
@@ -59,11 +59,15 @@ func List() []ClientAuth {
|
||||
}
|
||||
}
|
||||
|
||||
func Parse(s string) ClientAuth {
|
||||
func cleanString(s string) string {
|
||||
s = strings.ToLower(s)
|
||||
s = strings.Replace(s, "\"", "", -1)
|
||||
s = strings.Replace(s, "'", "", -1)
|
||||
s = strings.TrimSpace(s)
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
func Parse(s string) ClientAuth {
|
||||
s = cleanString(s)
|
||||
|
||||
switch {
|
||||
case strings.Contains(s, strict) || (strings.Contains(s, require) && strings.Contains(s, verify)):
|
||||
|
@@ -28,6 +28,7 @@ package auth
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
libmap "github.com/go-viper/mapstructure/v2"
|
||||
)
|
||||
@@ -48,13 +49,19 @@ func ViperDecoderHook() libmap.DecodeHookFuncType {
|
||||
}
|
||||
|
||||
// Check if the target type matches the expected one
|
||||
if to != reflect.TypeOf(z) {
|
||||
if to.Kind() != reflect.Int {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
if strings.Contains(cleanString(t), none) {
|
||||
return NoClientCert, nil
|
||||
}
|
||||
|
||||
// Format/decode/parse the data and return the new value
|
||||
if e := z.unmarshall([]byte(t)); e != nil {
|
||||
return nil, e
|
||||
return data, nil
|
||||
} else if z == NoClientCert {
|
||||
return data, nil
|
||||
} else {
|
||||
return z, nil
|
||||
}
|
||||
|
@@ -79,13 +79,13 @@ func ViperDecoderHook() libmap.DecodeHookFuncType {
|
||||
}
|
||||
|
||||
// Check if the target type matches the expected one
|
||||
if to != reflect.TypeOf(z) {
|
||||
if to.Kind() != reflect.Interface {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Format/decode/parse the data and return the new value
|
||||
if e := z.unMarshall([]byte(t)); e != nil {
|
||||
return nil, e
|
||||
return data, nil
|
||||
} else {
|
||||
return z, nil
|
||||
}
|
||||
|
@@ -38,13 +38,13 @@ import (
|
||||
func (o *Certif) unMarshall(p []byte) error {
|
||||
if o.UnmarshalJSON(p) == nil {
|
||||
return nil
|
||||
} else if o.UnmarshalYAML(&yaml.Node{Value: string(p)}) != nil {
|
||||
} else if o.UnmarshalYAML(&yaml.Node{Value: string(p)}) == nil {
|
||||
return nil
|
||||
} else if o.UnmarshalTOML(p) != nil {
|
||||
} else if o.UnmarshalTOML(p) == nil {
|
||||
return nil
|
||||
} else if o.UnmarshalCBOR(p) != nil {
|
||||
} else if o.UnmarshalCBOR(p) == nil {
|
||||
return nil
|
||||
} else if o.UnmarshalText(p) != nil {
|
||||
} else if o.UnmarshalText(p) == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@@ -28,6 +28,7 @@ package certs
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
|
||||
libmap "github.com/go-viper/mapstructure/v2"
|
||||
@@ -80,28 +81,28 @@ func (o *Certif) GetCerts() []string {
|
||||
func ViperDecoderHook() libmap.DecodeHookFuncType {
|
||||
return func(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) {
|
||||
var (
|
||||
ok bool
|
||||
z = &Certif{c: tls.Certificate{}}
|
||||
t string
|
||||
k bool
|
||||
)
|
||||
|
||||
// Check if the data type matches the expected one
|
||||
if from.Kind() != reflect.String {
|
||||
if _, ok = data.(map[string]interface{}); ok {
|
||||
if p, e := json.Marshal(data); e != nil {
|
||||
return data, nil
|
||||
} else if t, k = data.(string); !k {
|
||||
} else if e = z.UnmarshalJSON(p); e != nil {
|
||||
return data, nil
|
||||
} else {
|
||||
return z, nil
|
||||
}
|
||||
|
||||
// Check if the target type matches the expected one
|
||||
if to != reflect.TypeOf(z) {
|
||||
} else if _, ok = data.(string); !ok {
|
||||
if p, e := json.Marshal(data); e != nil {
|
||||
return data, nil
|
||||
} else if e = z.UnmarshalJSON(p); e != nil {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Format/decode/parse the data and return the new value
|
||||
if e := z.unMarshall([]byte(t)); e != nil {
|
||||
return nil, e
|
||||
} else {
|
||||
return z, nil
|
||||
}
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
}
|
||||
|
@@ -77,13 +77,15 @@ func ViperDecoderHook() libmap.DecodeHookFuncType {
|
||||
}
|
||||
|
||||
// Check if the target type matches the expected one
|
||||
if to != reflect.TypeOf(z) {
|
||||
if to.Kind() != reflect.Uint16 {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Format/decode/parse the data and return the new value
|
||||
if e := z.unmarshall([]byte(t)); e != nil {
|
||||
return nil, e
|
||||
return data, nil
|
||||
} else if z == Unknown {
|
||||
return data, nil
|
||||
} else {
|
||||
return z, nil
|
||||
}
|
||||
|
@@ -63,13 +63,15 @@ func ViperDecoderHook() libmap.DecodeHookFuncType {
|
||||
}
|
||||
|
||||
// Check if the target type matches the expected one
|
||||
if to != reflect.TypeOf(z) {
|
||||
if to.Kind() != reflect.Uint16 {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Format/decode/parse the data and return the new value
|
||||
if e := z.unmarshall([]byte(t)); e != nil {
|
||||
return nil, e
|
||||
return data, nil
|
||||
} else if z == Unknown {
|
||||
return data, nil
|
||||
} else {
|
||||
return z, nil
|
||||
}
|
||||
|
@@ -63,13 +63,15 @@ func ViperDecoderHook() libmap.DecodeHookFuncType {
|
||||
}
|
||||
|
||||
// Check if the target type matches the expected one
|
||||
if to != reflect.TypeOf(z) {
|
||||
if to.Kind() != reflect.Int {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Format/decode/parse the data and return the new value
|
||||
if e := z.unmarshall([]byte(t)); e != nil {
|
||||
return nil, e
|
||||
return data, nil
|
||||
} else if z == VersionUnknown {
|
||||
return data, nil
|
||||
} else {
|
||||
return z, nil
|
||||
}
|
||||
|
@@ -79,10 +79,15 @@ func ParseDuration(d time.Duration) Duration {
|
||||
}
|
||||
|
||||
func ParseFloat64(f float64) Duration {
|
||||
const mx float64 = math.MaxInt64
|
||||
const (
|
||||
mx float64 = math.MaxInt64
|
||||
mi = -mx
|
||||
)
|
||||
|
||||
if f > mx {
|
||||
return Duration(math.MaxInt64)
|
||||
} else if f < mi {
|
||||
return Duration(-math.MaxInt64)
|
||||
} else {
|
||||
return Duration(math.Round(f))
|
||||
}
|
||||
|
@@ -63,10 +63,15 @@ func ParseDuration(d time.Duration) Duration {
|
||||
}
|
||||
|
||||
func ParseFloat64(f float64) Duration {
|
||||
const mx float64 = math.MaxInt64
|
||||
const (
|
||||
mx float64 = math.MaxInt64
|
||||
mi = -mx
|
||||
)
|
||||
|
||||
if f > mx {
|
||||
return Duration(math.MaxInt64)
|
||||
} else if f < mi {
|
||||
return Duration(-math.MaxInt64)
|
||||
} else {
|
||||
return Duration(math.Round(f))
|
||||
}
|
||||
|
@@ -38,6 +38,8 @@ type Semaphore interface {
|
||||
context.Context
|
||||
semtps.Sem
|
||||
semtps.Progress
|
||||
|
||||
Clone() Semaphore
|
||||
}
|
||||
|
||||
func MaxSimultaneous() int {
|
||||
|
@@ -56,6 +56,13 @@ func (o *sem) Weighted() int64 {
|
||||
return o.s.Weighted()
|
||||
}
|
||||
|
||||
func (o *sem) Clone() Semaphore {
|
||||
return &sem{
|
||||
s: o.s.New(),
|
||||
m: o.m,
|
||||
}
|
||||
}
|
||||
|
||||
func (o *sem) New() semtps.Sem {
|
||||
return o.s.New()
|
||||
}
|
||||
|
Reference in New Issue
Block a user