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

报告相同问题?

悬赏问题

  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?