是否可以使用Yamlslurper和YamlBuilder查找重复的属性名称?

发布于 2025-02-03 06:25:04 字数 781 浏览 2 评论 0原文

如果我有这样的yaml文件:

obj:
  - name: 'msg'
    patterns:
      '("ssn":")([^"]*)(")' : '$1xxxxxxxxx$3'
      '("cvv":")([^"]*)(")' : '$1xxxx$3'
      '("ssn":")([^"]*)(")' : '$1xxxxxxxxx$3'

如果我使用groovy yamlslurper类来加载它,则“模式”对象将具有两个属性,其中包括以下名称:(

  • “ ssn”:“:”)([^'^“)*)([^'']*)( ”)
  • (“ cvv”:“)([^“^”]*)(“)

输入中的第三个属性具有与第一个属性名称相同的名称,因此第三个属性将覆盖第一个(或VICE反之亦然)。

有什么方法可以使用Yamlslurper或Yamlbuilder的某种组合来以某种方式检测到输入是否具有重复的属性名称?

我可以看到更改该文件的预期语法,以期期望这样的格式:

obj:
  - name: 'msg'
    patterns:
      - '("ssn":")([^"]*)(")' : '$1xxxxxxxxx$3'
      - '("cvv":")([^"]*)(")' : '$1xxxx$3'
      - '("ssn":")([^"]*)(")' : '$1xxxxxxxxx$3'

将使我能够检测重复。如果我无法使用原始格式在输入上检测重复的属性名称,则将考虑更改规格以需要修改格式。

If I have a yaml file like this:

obj:
  - name: 'msg'
    patterns:
      '("ssn":")([^"]*)(")' : '$1xxxxxxxxx$3'
      '("cvv":")([^"]*)(")' : '$1xxxx$3'
      '("ssn":")([^"]*)(")' : '$1xxxxxxxxx$3'

If I use the Groovy YamlSlurper class to load this, the "patterns" object will have two properties, with the following names:

  • ("ssn":")([^"]*)(")
  • ("cvv":")([^"]*)(")

The third property in the input has a name identical to the first property name, so the third one will override the first (or vice versa).

Is there any way I could use some combination of YamlSlurper and perhaps YamlBuilder to somehow detect that the input has a duplicate property name?

I can see that changing the expected syntax of this file so that it expects a format like this:

obj:
  - name: 'msg'
    patterns:
      - '("ssn":")([^"]*)(")' : '$1xxxxxxxxx$3'
      - '("cvv":")([^"]*)(")' : '$1xxxx$3'
      - '("ssn":")([^"]*)(")' : '$1xxxxxxxxx$3'

Would give me the ability to detect duplicates. If I can't detect duplicate property names on the input using the original format, I will consider changing the spec to require the modified format.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有心的人 2025-02-10 06:25:04

Groovy.yaml.yamlslurper有一个Jackson库。

yamlslurper.parse()
- > yamlconverter.convertyamltojson() - > Jackson

因此,您可以使用它来检测首先重复:

import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.DeserializationFeature

String yaml = '''
obj:
  - name: 'msg'
    patterns:
      '("ssn":")([^"]*)(")' : '$1xxxxxxxxx$3'
      '("cvv":")([^"]*)(")' : '$1xxxx$3'
      '("ssn":")([^"]*)(")' : '$1xxxxxxxxx$3'
'''

def result =
    new ObjectMapper()
        .enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)
        .readTree(new YAMLFactory().createParser(yaml))

此代码引发异常:

com.fasterxml.jackson.databind.exc.MismatchedInputException: 
  Duplicate field '("ssn":")([^"]*)(")' for `ObjectNode`: 
  not allowed when `DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY` enabled
  at [Source: (StringReader); line: 7, column: 31]

behind groovy.yaml.YamlSlurper there is a jackson library.

YamlSlurper.parse()
-> YamlConverter.convertYamlToJson() -> jackson

so, you could use it to detect first duplicate:

import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.DeserializationFeature

String yaml = '''
obj:
  - name: 'msg'
    patterns:
      '("ssn":")([^"]*)(")' : '$1xxxxxxxxx$3'
      '("cvv":")([^"]*)(")' : '$1xxxx$3'
      '("ssn":")([^"]*)(")' : '$1xxxxxxxxx$3'
'''

def result =
    new ObjectMapper()
        .enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)
        .readTree(new YAMLFactory().createParser(yaml))

this code throws exception:

com.fasterxml.jackson.databind.exc.MismatchedInputException: 
  Duplicate field '("ssn":")([^"]*)(")' for `ObjectNode`: 
  not allowed when `DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY` enabled
  at [Source: (StringReader); line: 7, column: 31]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文