parser.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package jsonified
  2. import (
  3. "github.com/v2fly/v2ray-core/v5/app/subscription/containers"
  4. "github.com/v2fly/v2ray-core/v5/app/subscription/containers/jsonfieldarray"
  5. "github.com/v2fly/v2ray-core/v5/common"
  6. jsonConf "github.com/v2fly/v2ray-core/v5/infra/conf/json"
  7. )
  8. func newJsonifiedYamlParser() containers.SubscriptionContainerDocumentParser {
  9. return &jsonifiedYAMLParser{}
  10. }
  11. type jsonifiedYAMLParser struct{}
  12. func (j jsonifiedYAMLParser) ParseSubscriptionContainerDocument(rawConfig []byte) (*containers.Container, error) {
  13. parser := jsonfieldarray.NewJSONFieldArrayParser()
  14. jsonified, err := jsonConf.FromYAML(rawConfig)
  15. if err != nil {
  16. return nil, newError("failed to parse as yaml").Base(err)
  17. }
  18. container, err := parser.ParseSubscriptionContainerDocument(jsonified)
  19. if err != nil {
  20. return nil, newError("failed to parse as jsonfieldarray").Base(err)
  21. }
  22. container.Kind = "Yaml2Json+" + container.Kind
  23. for _, value := range container.ServerSpecs {
  24. value.KindHint = "Yaml2Json+" + value.KindHint
  25. }
  26. return container, nil
  27. }
  28. func init() {
  29. common.Must(containers.RegisterParser("Yaml2Json", newJsonifiedYamlParser()))
  30. }