1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-20 08:16:55 -05:00
v2fly/commands/all/merge_doc.go
Jebbs ff59bd37ce
v5: New multi-json loader (#451)
* scalable commands column

* new multi-json loader
For both internal & external json loader

This commit also:
* applies -confdir to other formats, e.g. "yaml" in the future
* multiple assign of -confdir is accepted
* add flag to load confdir recursively
* config loader can have alias name
* json loader also accepts .jsonc
* add merge command
* add help topics for json merge, format loader
* format loaders don't panic

* apply lint style

* add merge test

* merge same tag in array, solve v2fly/discussion#97

* apply lint style

* merge code optimize

* fix merge cmdarg.Arg

* update cmd description

* improve merge logic
* fix zero value overwrite
* fix "null" lost after array merge

* code optimize

* fix merged slices not sorted

* code optimize

* add package doc

* fix a typo
2020-11-28 22:06:03 +08:00

67 lines
1.6 KiB
Go

package all
import (
"v2ray.com/core/commands/base"
)
var docMerge = &base.Command{
UsageLine: "{{.Exec}} json-merge",
Short: "json merge logic",
Long: `
Merging of JSON configs is applied in following commands:
{{.Exec}} run -c c1.json -c c2.json ...
{{.Exec}} merge c1.json https://url.to/c2.json ...
{{.Exec}} convert c1.json dir1 ...
Suppose we have 2 JSON files,
The 1st one:
{
"log": {"access": "some_value", "loglevel": "debug"},
"inbounds": [{"tag": "in-1"}],
"outbounds": [{"_priority": 100, "tag": "out-1"}],
"routing": {"rules": [
{"_tag":"default_route","inboundTag":["in-1"],"outboundTag":"out-1"}
]}
}
The 2nd one:
{
"log": {"loglevel": "error"},
"inbounds": [{"tag": "in-2"}],
"outbounds": [{"_priority": -100, "tag": "out-2"}],
"routing": {"rules": [
{"inboundTag":["in-2"],"outboundTag":"out-2"},
{"_tag":"default_route","inboundTag":["in-1.1"],"outboundTag":"out-1.1"}
]}
}
Output:
{
// loglevel is overwritten
"log": {"access": "some_value", "loglevel": "error"},
"inbounds": [{"tag": "in-1"}, {"tag": "in-2"}],
"outbounds": [
{"tag": "out-2"}, // note the order is affected by priority
{"tag": "out-1"}
],
"routing": {"rules": [
// note 3 rules are merged into 2, and outboundTag is overwritten,
// because 2 of them has same tag
{"inboundTag":["in-1","in-1.1"],"outboundTag":"out-1.1"}
{"inboundTag":["in-2"],"outboundTag":"out-2"}
]}
}
Explained:
- Simple values (string, number, boolean) are overwritten, others are merged
- Elements with same "tag" (or "_tag") in an array will be merged
- Add "_priority" property to array elements will help sort the array
`,
}