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 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: