vendor and go mod. add ldap file
This commit is contained in:
176
vendor/github.com/pyke369/golang-support/uconfig/uconfig.go
generated
vendored
176
vendor/github.com/pyke369/golang-support/uconfig/uconfig.go
generated
vendored
@@ -14,11 +14,12 @@ import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/pyke369/golang-support/rcache"
|
||||
)
|
||||
|
||||
type UConfig struct {
|
||||
@@ -36,7 +37,6 @@ type replacer struct {
|
||||
}
|
||||
|
||||
var (
|
||||
cores int
|
||||
escaped string
|
||||
unescaper *regexp.Regexp
|
||||
requoter *regexp.Regexp
|
||||
@@ -49,27 +49,27 @@ var (
|
||||
|
||||
func init() {
|
||||
escaped = "{}[],#/*;:= " // match characters within quotes to escape
|
||||
unescaper = regexp.MustCompile("@\\d+@") // match escaped characters (to reverse previous escaping)
|
||||
expander = regexp.MustCompile("{{([<|@&!\\-\\+])\\s*([^{}]*?)\\s*}}") // match external content macros
|
||||
sizer = regexp.MustCompile("^(\\d+(?:\\.\\d*)?)\\s*([KMGTP]?)(B?)$") // match size value
|
||||
duration1 = regexp.MustCompile("(\\d+)(Y|MO|D|H|MN|S|MS|US)?") // match duration value form1 (free)
|
||||
duration2 = regexp.MustCompile("^(?:(\\d+):)?(\\d{2}):(\\d{2})(?:\\.(\\d{1,3}))?$") // match duration value form2 (timecode)
|
||||
replacers[0] = replacer{regexp.MustCompile("(?m)^(.*?)(?:#|//).*?$"), "$1", false} // remove # and // commented portions
|
||||
replacers[1] = replacer{regexp.MustCompile("/\\*[^\\*]*\\*/"), "", true} // remove /* */ commented portions
|
||||
replacers[2] = replacer{regexp.MustCompile("(?m)^\\s+"), "", false} // trim leading spaces
|
||||
replacers[3] = replacer{regexp.MustCompile("(?m)\\s+$"), "", false} // trim trailing spaces
|
||||
unescaper = regexp.MustCompile(`@\d+@`) // match escaped characters (to reverse previous escaping)
|
||||
expander = regexp.MustCompile(`{{([<|@&!\-\+])\s*([^{}]*?)\s*}}`) // match external content macros
|
||||
sizer = regexp.MustCompile(`^(\d+(?:\.\d*)?)\s*([KMGTP]?)(B?)$`) // match size value
|
||||
duration1 = regexp.MustCompile(`(\d+)(Y|MO|D|H|MN|S|MS|US)?`) // match duration value form1 (free)
|
||||
duration2 = regexp.MustCompile(`^(?:(\d+):)?(\d{2}):(\d{2})(?:\.(\d{1,3}))?$`) // match duration value form2 (timecode)
|
||||
replacers[0] = replacer{regexp.MustCompile("(?m)^(.*?)(?:#|//).*?$"), `$1`, false} // remove # and // commented portions
|
||||
replacers[1] = replacer{regexp.MustCompile(`/\*[^\*]*\*/`), ``, true} // remove /* */ commented portions
|
||||
replacers[2] = replacer{regexp.MustCompile(`(?m)^\s+`), ``, false} // trim leading spaces
|
||||
replacers[3] = replacer{regexp.MustCompile(`(?m)\s+$`), ``, false} // trim trailing spaces
|
||||
replacers[4] = replacer{regexp.MustCompile("(?s)(^|[\r\n]+)\\[([^\\]\r\n]+?)\\](.+?)((?:[\r\n]+\\[)|$)"), "$1$2\n{$3\n}$4", true} // convert INI sections into JSON objects
|
||||
replacers[5] = replacer{regexp.MustCompile("(?m)^(\\S+)\\s+([^{}\\[\\],;:=]+);$"), "$1 = $2;", false} // add missing key-value separators
|
||||
replacers[6] = replacer{regexp.MustCompile("(?m);$"), ",", false} // replace ; line terminators by ,
|
||||
replacers[7] = replacer{regexp.MustCompile("(\\S+?)\\s*[:=]"), "$1:", false} // replace = key-value separators by :
|
||||
replacers[8] = replacer{regexp.MustCompile("([}\\]])(\\s*)([^,}\\]\\s])"), "$1,$2$3", false} // add missing objects/arrays , separators
|
||||
replacers[9] = replacer{regexp.MustCompile("(?m)(^[^:]+:.+?[^,])$"), "$1,", false} // add missing values trailing , seperators
|
||||
replacers[10] = replacer{regexp.MustCompile("(^|[,{\\[]+\\s*)([^:{\\[]+?)(\\s*[{\\[])"), "$1$2:$3", true} // add missing key-(object/array-)value separator
|
||||
replacers[11] = replacer{regexp.MustCompile("(?m)^([^\":{}\\[\\]]+)"), "\"$1\"", false} // add missing quotes around keys
|
||||
replacers[12] = replacer{regexp.MustCompile("([:,\\[\\s]+)([^\",\\[\\]{}\n\r]+?)(\\s*[,\\]}])"), "$1\"$2\"$3", false} // add missing quotes around values
|
||||
replacers[5] = replacer{regexp.MustCompile(`(?m)^(\S+)\s+([^{}\[\],;:=]+);$`), "$1 = $2;", false} // add missing key-value separators
|
||||
replacers[6] = replacer{regexp.MustCompile(`(?m);$`), `,`, false} // replace ; line terminators by ,
|
||||
replacers[7] = replacer{regexp.MustCompile(`(\S+?)\s*[:=]`), `$1:`, false} // replace = key-value separators by :
|
||||
replacers[8] = replacer{regexp.MustCompile(`([}\]])(\s*)([^,}\]\s])`), `$1,$2$3`, false} // add missing objects/arrays , separators
|
||||
replacers[9] = replacer{regexp.MustCompile("(?m)(^[^:]+:.+?[^,])$"), `$1,`, false} // add missing values trailing , seperators
|
||||
replacers[10] = replacer{regexp.MustCompile(`(^|[,{\[]+\s*)([^:{\[]+?)(\s*[{\[])`), `$1$2:$3`, true} // add missing key-(object/array-)value separator
|
||||
replacers[11] = replacer{regexp.MustCompile(`(?m)^([^":{}\[\]]+)`), `"$1"`, false} // add missing quotes around keys
|
||||
replacers[12] = replacer{regexp.MustCompile("([:,\\[\\s]+)([^\",\\[\\]{}\n\r]+?)(\\s*[,\\]}])"), `$1"$2"$3`, false} // add missing quotes around values
|
||||
replacers[13] = replacer{regexp.MustCompile("\"[\r\n]"), "\",\n", false} // add still issing objects/arrays , separators
|
||||
replacers[14] = replacer{regexp.MustCompile("\"\\s*(.+?)\\s*\""), "\"$1\"", false} // trim leading and trailing spaces in quoted strings
|
||||
replacers[15] = replacer{regexp.MustCompile(",+(\\s*[}\\]])"), "$1", false} // remove objets/arrays last element extra ,
|
||||
replacers[14] = replacer{regexp.MustCompile(`"\s*(.+?)\s*"`), `"$1"`, false} // trim leading and trailing spaces in quoted strings
|
||||
replacers[15] = replacer{regexp.MustCompile(`,+(\s*[}\]])`), `$1`, false} // remove objets/arrays last element extra ,
|
||||
}
|
||||
|
||||
func escape(input string) string {
|
||||
@@ -77,7 +77,7 @@ func escape(input string) string {
|
||||
|
||||
instring := false
|
||||
for index := 0; index < len(input); index++ {
|
||||
if input[index:index+1] == "\"" && (index == 0 || input[index-1:index] != "\\") {
|
||||
if input[index:index+1] == `"` && (index == 0 || input[index-1:index] != `\`) {
|
||||
instring = !instring
|
||||
}
|
||||
if instring == true {
|
||||
@@ -135,9 +135,6 @@ func reduce(input interface{}) {
|
||||
}
|
||||
|
||||
func New(input string, inline ...bool) (*UConfig, error) {
|
||||
if cores == 0 {
|
||||
cores = runtime.NumCPU()
|
||||
}
|
||||
config := &UConfig{
|
||||
config: nil,
|
||||
}
|
||||
@@ -145,11 +142,7 @@ func New(input string, inline ...bool) (*UConfig, error) {
|
||||
}
|
||||
|
||||
func (this *UConfig) Load(input string, inline ...bool) error {
|
||||
if cores > 1 {
|
||||
this.Lock()
|
||||
defer this.Unlock()
|
||||
}
|
||||
|
||||
this.Lock()
|
||||
this.cache = map[string]interface{}{}
|
||||
base, _ := os.Getwd()
|
||||
content := fmt.Sprintf("/*base:%s*/\n", base)
|
||||
@@ -272,30 +265,29 @@ func (this *UConfig) Load(input string, inline ...bool) error {
|
||||
if syntax, ok := err.(*json.SyntaxError); ok && syntax.Offset < int64(len(content)) {
|
||||
if start := strings.LastIndex(content[:syntax.Offset], "\n") + 1; start >= 0 {
|
||||
line := strings.Count(content[:start], "\n") + 1
|
||||
this.Unlock()
|
||||
return errors.New(fmt.Sprintf("%s at line %d near %s", syntax, line, content[start:syntax.Offset]))
|
||||
}
|
||||
}
|
||||
this.Unlock()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
reduce(this.config)
|
||||
this.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *UConfig) Loaded() bool {
|
||||
if cores > 1 {
|
||||
this.RLock()
|
||||
defer this.RUnlock()
|
||||
}
|
||||
this.RLock()
|
||||
defer this.RUnlock()
|
||||
return !(this.config == nil)
|
||||
}
|
||||
|
||||
func (this *UConfig) Hash() string {
|
||||
if cores > 1 {
|
||||
this.RLock()
|
||||
defer this.RUnlock()
|
||||
}
|
||||
this.RLock()
|
||||
defer this.RUnlock()
|
||||
return this.hash
|
||||
}
|
||||
|
||||
@@ -312,54 +304,41 @@ func (this *UConfig) GetPaths(path string) []string {
|
||||
paths []string = []string{}
|
||||
)
|
||||
|
||||
if cores > 1 {
|
||||
this.RLock()
|
||||
defer this.RUnlock()
|
||||
}
|
||||
this.RLock()
|
||||
prefix := ""
|
||||
if current == nil || path == "" {
|
||||
this.RUnlock()
|
||||
return paths
|
||||
}
|
||||
if cores > 1 {
|
||||
this.cacheLock.RLock()
|
||||
}
|
||||
this.cacheLock.RLock()
|
||||
if this.cache[path] != nil {
|
||||
if paths, ok := this.cache[path].([]string); ok {
|
||||
if cores > 1 {
|
||||
this.cacheLock.RUnlock()
|
||||
}
|
||||
this.cacheLock.RUnlock()
|
||||
this.RUnlock()
|
||||
return paths
|
||||
}
|
||||
}
|
||||
if cores > 1 {
|
||||
this.cacheLock.RUnlock()
|
||||
}
|
||||
this.cacheLock.RUnlock()
|
||||
if path != "" {
|
||||
prefix = "."
|
||||
for _, part := range strings.Split(path, ".") {
|
||||
kind := reflect.TypeOf(current).Kind()
|
||||
index, err := strconv.Atoi(part)
|
||||
if (kind == reflect.Slice && (err != nil || index < 0 || index >= len(current.([]interface{})))) || (kind != reflect.Slice && kind != reflect.Map) {
|
||||
if cores > 1 {
|
||||
this.cacheLock.Lock()
|
||||
}
|
||||
this.cacheLock.Lock()
|
||||
this.cache[path] = paths
|
||||
if cores > 1 {
|
||||
this.cacheLock.Unlock()
|
||||
}
|
||||
this.cacheLock.Unlock()
|
||||
this.RUnlock()
|
||||
return paths
|
||||
}
|
||||
if kind == reflect.Slice {
|
||||
current = current.([]interface{})[index]
|
||||
} else {
|
||||
if current = current.(map[string]interface{})[strings.TrimSpace(part)]; current == nil {
|
||||
if cores > 1 {
|
||||
this.cacheLock.Lock()
|
||||
}
|
||||
this.cacheLock.Lock()
|
||||
this.cache[path] = paths
|
||||
if cores > 1 {
|
||||
this.cacheLock.Unlock()
|
||||
}
|
||||
this.cacheLock.Unlock()
|
||||
this.RUnlock()
|
||||
return paths
|
||||
}
|
||||
}
|
||||
@@ -375,91 +354,68 @@ func (this *UConfig) GetPaths(path string) []string {
|
||||
paths = append(paths, fmt.Sprintf("%s%s%s", path, prefix, key))
|
||||
}
|
||||
}
|
||||
if cores > 1 {
|
||||
this.cacheLock.Lock()
|
||||
}
|
||||
this.cacheLock.Lock()
|
||||
this.cache[path] = paths
|
||||
if cores > 1 {
|
||||
this.cacheLock.Unlock()
|
||||
}
|
||||
this.cacheLock.Unlock()
|
||||
this.RUnlock()
|
||||
return paths
|
||||
}
|
||||
|
||||
func (this *UConfig) value(path string) (string, error) {
|
||||
var current interface{} = this.config
|
||||
|
||||
if cores > 1 {
|
||||
this.RLock()
|
||||
defer this.RUnlock()
|
||||
}
|
||||
this.RLock()
|
||||
if current == nil || path == "" {
|
||||
this.RUnlock()
|
||||
return "", fmt.Errorf("invalid parameter")
|
||||
}
|
||||
if cores > 1 {
|
||||
this.cacheLock.RLock()
|
||||
}
|
||||
this.cacheLock.RLock()
|
||||
if this.cache[path] != nil {
|
||||
if current, ok := this.cache[path].(bool); ok && !current {
|
||||
if cores > 1 {
|
||||
this.cacheLock.RUnlock()
|
||||
}
|
||||
this.cacheLock.RUnlock()
|
||||
this.RUnlock()
|
||||
return "", fmt.Errorf("invalid path")
|
||||
}
|
||||
if current, ok := this.cache[path].(string); ok {
|
||||
if cores > 1 {
|
||||
this.cacheLock.RUnlock()
|
||||
}
|
||||
this.cacheLock.RUnlock()
|
||||
this.RUnlock()
|
||||
return current, nil
|
||||
}
|
||||
}
|
||||
if cores > 1 {
|
||||
this.cacheLock.RUnlock()
|
||||
}
|
||||
this.cacheLock.RUnlock()
|
||||
for _, part := range strings.Split(path, ".") {
|
||||
kind := reflect.TypeOf(current).Kind()
|
||||
index, err := strconv.Atoi(part)
|
||||
if (kind == reflect.Slice && (err != nil || index < 0 || index >= len(current.([]interface{})))) || (kind != reflect.Slice && kind != reflect.Map) {
|
||||
if cores > 1 {
|
||||
this.cacheLock.Lock()
|
||||
}
|
||||
this.cacheLock.Lock()
|
||||
this.cache[path] = false
|
||||
if cores > 1 {
|
||||
this.cacheLock.Unlock()
|
||||
}
|
||||
this.cacheLock.Unlock()
|
||||
this.RUnlock()
|
||||
return "", fmt.Errorf("invalid path")
|
||||
}
|
||||
if kind == reflect.Slice {
|
||||
current = current.([]interface{})[index]
|
||||
} else {
|
||||
if current = current.(map[string]interface{})[strings.TrimSpace(part)]; current == nil {
|
||||
if cores > 1 {
|
||||
this.cacheLock.Lock()
|
||||
}
|
||||
this.cacheLock.Lock()
|
||||
this.cache[path] = false
|
||||
if cores > 1 {
|
||||
this.cacheLock.Unlock()
|
||||
}
|
||||
this.cacheLock.Unlock()
|
||||
this.RUnlock()
|
||||
return "", fmt.Errorf("invalid path")
|
||||
}
|
||||
}
|
||||
}
|
||||
if reflect.TypeOf(current).Kind() == reflect.String {
|
||||
if cores > 1 {
|
||||
this.cacheLock.Lock()
|
||||
}
|
||||
this.cacheLock.Lock()
|
||||
this.cache[path] = current.(string)
|
||||
if cores > 1 {
|
||||
this.cacheLock.Unlock()
|
||||
}
|
||||
this.cacheLock.Unlock()
|
||||
this.RUnlock()
|
||||
return current.(string), nil
|
||||
}
|
||||
if cores > 1 {
|
||||
this.cacheLock.Lock()
|
||||
}
|
||||
this.cacheLock.Lock()
|
||||
this.cache[path] = false
|
||||
if cores > 1 {
|
||||
this.cacheLock.Unlock()
|
||||
}
|
||||
this.cacheLock.Unlock()
|
||||
this.RUnlock()
|
||||
return "", fmt.Errorf("invalid path")
|
||||
}
|
||||
|
||||
@@ -486,7 +442,7 @@ func (this *UConfig) GetStringMatchCaptures(path string, fallback, match string)
|
||||
return []string{fallback}
|
||||
}
|
||||
if match != "" {
|
||||
if matcher, err := regexp.Compile(match); err == nil {
|
||||
if matcher := rcache.Get(match); matcher != nil {
|
||||
if matches := matcher.FindStringSubmatch(value); matches != nil {
|
||||
return matches
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user