duandang9434 2018-03-23 05:16
浏览 1131
已采纳

使用golang验证yaml模式(语义检查)

We have tool which need to read YAML file with specific structure. When we got the YAML file we need to know if

  1. Check if the YAML file is valid according to some guideline - semantic check
  2. Where is the syntax error if any

For example this is example of the validation that we need to address

 _version:  {required: true}
   id: {required: true, pattern: '/^[A-Za_\-\.]+$/'}   
   release-version: {required: true}
   type:   

   builds:
     type:seq
     sequence:
       -type:map
     mapping:
        name:{required: true, unique: true, pattern: '/^[A-Za-z0-3_\-\.]+$/'}
        params: 
          type: map
          mapping: { =: {type: any} } 

Mapping is a key value object
seq can have multiple builds
type any is and key value

We use this open source to parse the yaml https://github.com/go-yaml/yaml

One idea (which is good) is to convert to json like following to do it by converting the file to json and validate it which have library to support it, any example in my context will be very helpful https://github.com/xeipuuv/gojsonschema

But not sure how I handle

Type map
Type seq
  • 写回答

2条回答 默认 最新

  • dongzhen7108 2018-03-23 08:01
    关注

    Here is what you could try.

    Model a struct after the shape of the expected yaml data:

    type Config struct {
            Version struct {
                    Required bool
            }
            ID struct {
                    Required bool
                    Pattern string
            }
            ReleaseVersion struct {
                    Required bool
            }
            Type interface{}
            Builds struct {
                    Type []interface{} `yaml:"type"`
                    Sequence struct {
                            Type string
                    }
                    Mapping struct {
                            Name map[string]interface{}
                            Params struct {
                                    Type string `yaml:"type"`
                                    Mapping struct {
                                            To map[string]string `yaml:"="`
                                    }
                            }
                    } `yaml:"mapping"`              
            }
    }
    

    The yaml flag yaml:"somefield" is added to label the field name of the yaml the data we're interested in.

    Also many fields with unknown/undetermined type can be declared as empty interface (interface{}) or if you want to "enforce" that the underlying form is a key-value object you can either declare it as a map[string]interface{} or another struct.

    We then unmarshal the yaml data into the struct:

    cfg := Config{}
    err := yaml.Unmarshal([]byte(data), &cfg)
    if err != nil {
            log.Fatalf("error: %v", err)
    }
    

    Since we have modeled fields as either anonymous structs or maps, we can test if a particular field has a "key-value" value by checking its equality to nil.

    // Mapping is a key value object
    if (Mapping != nil) {
            // Mapping is a key-value object, since it's not nil.
    }
    
    
    // type any is and key value
    // Mapping.To is declared with map[string]string type
    // so if it's not nil we can say there's a map there.
    if (Mapping.To != nil) {
            // Mapping.To is a map
    }
    

    In marshaling/unmarshaling, maps and structs are pretty interchangeable. The benefit of a struct is you can predefine the field's names ahead of time while unmarshaling to a map it won't be clear to you what the keys are.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格