doutong7216 2018-06-29 21:11
浏览 330
已采纳

解析并验证“ key1:value1; key2:value2”字符串有效地进行结构化?

I have a "key1:value1; key2:value2" like string (string with key:value pattern concated by ;).

Now I wish to parse this string to a Go struct:

type CustomStruct struct {
   KeyName1 string `name:"key1" somevalidation:"xxx"`
   KeyName2 int    `name:"key2" somevalidation:"yyy"`
}

In the above example, the struct tag defines the name of the key in the string and can provide some validation for its corresponding value (it can set a default value if validation fails). For instance, KeyName2 is an int value, so I wish the somevalidation can check whether the KeyName2 satisfy, let's say, greater than 30 and less equal than 100.

And in another senario, I can define another struct CustomStruct2 for string like key3:value3; key4:value4;

How can I archive this kind of requirement efficiently and elegantly?

  • 写回答

1条回答 默认 最新

  • dph58509 2018-06-30 06:03
    关注

    I'll assume that you can parse the data to a map[string]interface{}.

    Use the reflect package to set the fields. Here's the basic function:

    // set sets fields in struct pointed to by pv to values in data.
    func set(pv interface{}, data map[string]interface{}) {
        // pv is assumed to be pointer to a struct
        s := reflect.ValueOf(pv).Elem()
    
        // Loop through fields
        t := s.Type()
        for i := 0; i < t.NumField(); i++ {
    
            // Set field if there's a data value for the field.
            f := t.Field(i)
            if d, ok := data[f.Tag.Get("name")]; ok {
                s.Field(i).Set(reflect.ValueOf(d))
            }
        }
    }
    

    This code assumes that the values in the data map are assignable to the corresponding field in the struct and that the first argument is a pointer to a struct. The code will panic if these assumptions are not true. You can protect against this by checking types and assignability using the reflect package.

    playground example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波