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:
Nicolas JUHEL
2025-04-10 17:38:45 +02:00
parent 19d64346f6
commit 3562c0bf86
12 changed files with 74 additions and 37 deletions

View File

@@ -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)):

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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 (
z = &Certif{c: tls.Certificate{}}
t string
k bool
ok bool
z = &Certif{c: tls.Certificate{}}
)
// Check if the data type matches the expected one
if from.Kind() != reflect.String {
return data, nil
} else if t, k = data.(string); !k {
return data, nil
if _, ok = data.(map[string]interface{}); ok {
if p, e := json.Marshal(data); e != nil {
return data, nil
} else if e = z.UnmarshalJSON(p); e != nil {
return data, nil
} else {
return z, nil
}
} 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
} else {
return z, nil
}
}
// Check if the target type matches the expected one
if to != reflect.TypeOf(z) {
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
}
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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))
}

View File

@@ -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))
}

View File

@@ -38,6 +38,8 @@ type Semaphore interface {
context.Context
semtps.Sem
semtps.Progress
Clone() Semaphore
}
func MaxSimultaneous() int {

View File

@@ -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()
}