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

使用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条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样