merge_doc.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package all
  2. import (
  3. "github.com/v2fly/v2ray-core/v4/commands/base"
  4. )
  5. var docMerge = &base.Command{
  6. UsageLine: "{{.Exec}} json-merge",
  7. Short: "json merge logic",
  8. Long: `
  9. Merging of JSON configs is applied in following commands:
  10. {{.Exec}} run -c c1.json -c c2.json ...
  11. {{.Exec}} merge c1.json https://url.to/c2.json ...
  12. {{.Exec}} convert c1.json dir1 ...
  13. Suppose we have 2 JSON files,
  14. The 1st one:
  15. {
  16. "log": {"access": "some_value", "loglevel": "debug"},
  17. "inbounds": [{"tag": "in-1"}],
  18. "outbounds": [{"_priority": 100, "tag": "out-1"}],
  19. "routing": {"rules": [
  20. {"_tag":"default_route","inboundTag":["in-1"],"outboundTag":"out-1"}
  21. ]}
  22. }
  23. The 2nd one:
  24. {
  25. "log": {"loglevel": "error"},
  26. "inbounds": [{"tag": "in-2"}],
  27. "outbounds": [{"_priority": -100, "tag": "out-2"}],
  28. "routing": {"rules": [
  29. {"inboundTag":["in-2"],"outboundTag":"out-2"},
  30. {"_tag":"default_route","inboundTag":["in-1.1"],"outboundTag":"out-1.1"}
  31. ]}
  32. }
  33. Output:
  34. {
  35. // loglevel is overwritten
  36. "log": {"access": "some_value", "loglevel": "error"},
  37. "inbounds": [{"tag": "in-1"}, {"tag": "in-2"}],
  38. "outbounds": [
  39. {"tag": "out-2"}, // note the order is affected by priority
  40. {"tag": "out-1"}
  41. ],
  42. "routing": {"rules": [
  43. // note 3 rules are merged into 2, and outboundTag is overwritten,
  44. // because 2 of them has same tag
  45. {"inboundTag":["in-1","in-1.1"],"outboundTag":"out-1.1"}
  46. {"inboundTag":["in-2"],"outboundTag":"out-2"}
  47. ]}
  48. }
  49. Explained:
  50. - Simple values (string, number, boolean) are overwritten, others are merged
  51. - Elements with same "tag" (or "_tag") in an array will be merged
  52. - Add "_priority" property to array elements will help sort the array
  53. `,
  54. }