2020-11-28 14:06:03 +00:00
|
|
|
// Copyright 2020 Jebbs. All rights reserved.
|
|
|
|
// Use of this source code is governed by MIT
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package merge
|
|
|
|
|
|
|
|
const priorityKey string = "_priority"
|
|
|
|
const tagKey string = "_tag"
|
|
|
|
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
// ApplyRules applies merge rules according to _tag, _priority fields, and remove them
|
|
|
|
func ApplyRules(m map[string]interface{}) error {
|
2020-11-28 14:06:03 +00:00
|
|
|
err := sortMergeSlices(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
removeHelperFields(m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortMergeSlices enumerates all slices in a map, to sort by priority and merge by tag
|
|
|
|
func sortMergeSlices(target map[string]interface{}) error {
|
|
|
|
for key, value := range target {
|
|
|
|
if slice, ok := value.([]interface{}); ok {
|
|
|
|
sortByPriority(slice)
|
|
|
|
s, err := mergeSameTag(slice)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
target[key] = s
|
|
|
|
for _, item := range s {
|
|
|
|
if m, ok := item.(map[string]interface{}); ok {
|
|
|
|
sortMergeSlices(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if field, ok := value.(map[string]interface{}); ok {
|
|
|
|
sortMergeSlices(field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeHelperFields(target map[string]interface{}) {
|
|
|
|
for key, value := range target {
|
|
|
|
if key == priorityKey || key == tagKey {
|
|
|
|
delete(target, key)
|
|
|
|
} else if slice, ok := value.([]interface{}); ok {
|
|
|
|
for _, e := range slice {
|
|
|
|
if el, ok := e.(map[string]interface{}); ok {
|
|
|
|
removeHelperFields(el)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if field, ok := value.(map[string]interface{}); ok {
|
|
|
|
removeHelperFields(field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|