各类软件的版本号定义虽然都不尽相同,但是其基本原理基本上还是相通的:通过特写的字符对字符串进行分割。我们把这一规则稍作整理,放到 struct tag 中,告诉解析器如何解析,下面就以 semver 为例作个示范:

type SemVersion struct {
Major int `version:"0,.1"`
Minor int `version:"1,.2"`
Patch int `version:"2,+4,-3"`
PreRelease string `version:"3,+4"`
Build string `version:"4"`
}
  1. 其中 struct tag 中的第一段内容表示的是当前字段的一个编号,要求唯一且为数值,0 表示入口;
  2. 后面的是解析规则,可以有多条,以逗号分隔,优先级等同;
  3. 每一条规则的第一个字符为触发条件,之后的数字即为前面的编号,当解析器碰到该字符时,即结束当前字段的解析,跳转到其后面指定的编号字段。

如何实现

首先定义一个表示每个字段的结构:

 type struct field {
value reflect.Value // 指赂字段的值
routes map[byte]int // 解析的跳转规则
}

然后将整个结构体解析到一个 map 中,其键名即为字段的编号:

func getFields(obj interface{}) (map[int]*field, error) {
v := reflect.ValueOf(obj)
t := v.Type()
fields := make(map[int]*field, v.NumField()) for i := 0; i < v.NumField(); i++ {
tags := strings.Split(t.Field(i).Tag.Get("version"), ",")
if len(tags) < 1 {
return nil, errors.New("缺少标签内容")
} index, err := strconv.Atoi(tags[0])
if err != nil {
return nil, err
}
if _, found := fields[index]; found {
return nil, errors.New("重复的字段编号")
} field := &field{routes: make(map[byte]int, 2)} for _, vv := range tags[1:] {
n, err := strconv.Atoi(vv[1:])
if err != nil {
return nil, err
}
field.routes[vv[0]] = n
} field.value = v.Field(i)
fields[index] = field
} return fields, nil
}

然后通过一个函数将字符串解析到结构中:

func Parse(obj interface{}, ver string) {
fields, _ := getFields(obj) start := 0
field := fields[0]
for i := 0; i < len(ver)+1; i++ {
var nextIndex int
if i < len(ver) { // 未结束
index, found := field.routes[ver[i]]
if !found {
continue
}
nextIndex = index
} switch field.value.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
n, err := strconv.ParseInt(ver[start:i], 10, 64)
if err != nil {
panic(err)
}
field.value.SetInt(n)
case reflect.String:
field.value.SetString(ver[start:i])
default:
panic("无效的类型")
} i++ // 过滤掉当前字符
start = i
field = fields[nextIndex] // 下一个 field
} // end for
}

之后我们只需要定义各类版本号的相关结构体,然后传给 Parse 就可以了:

// Major_Version_Number.Minor_Version_Number[.Revision_Number[.Build_Number]]
type GNUVersion struct {
Major int `version:"0,.1"`
Minor int `version:"1,.2"`
Revision int `version:"2, 3"`
Build string `version:"3"`
} gnu := &GNUVersion{}
sem := &SemVersion{}
Parse(gnu, "1.2.0 build-1124")
Parse(sem, "1.2.0+20160615")

查看完整的实现:https://github.com/issue9/version

使用 Go 的 struct tag 来解析版本号字符串的更多相关文章

  1. 【linux】U-BOOT与linux kernel通信: struct tag

      欢迎转载,转载时需保留作者信息. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http://b ...

  2. 引用 U-boot给kernel传参数和kernel读取参数—struct tag

    引用 清风徐徐 的 U-boot给kernel传参数和kernel读取参数—struct tag U-boot会给Linux Kernel传递很多参数,如:串口,RAM,videofb等.而Linux ...

  3. Go语言中的struct tag

    有时在Go的结构体定义时会看到这样的形式: type User struct { UserId int `json:"user_id" bson:"b_user_id&q ...

  4. java后台处理解析json字符串的两种方式

    简单说一下背景 上次后端通过模拟http请求百度地图接口,得到的是一个json字符串,而我只需要其中的某个key对应的value. 当时我是通过截取字符串取的,后来觉得不太合理,今天整理出了两种处理解 ...

  5. js中解析json对象:JSON.parse()用于从一个字符串中解析出json对象, JSON.stringify()用于从一个对象解析出字符串。

    JSON.parse()用于从一个字符串中解析出json对象. var str = '{"name":"huangxiaojian","age&quo ...

  6. 使用 dynamic 标记解析JSON字符串 JDynamic :支持Json反序列化为Dynamic对象

    使用 dynamic 标记解析JSON字符串  http://www.cnblogs.com/taotaodetuer/p/4171327.html 1 string jsonStr = " ...

  7. 字串符相关 split() 字串符分隔 substring() 提取字符串 substr()提取指定数目的字符 parseInt() 函数可解析一个字符串,并返回一个整数。

    split() 方法将字符串分割为字符串数组,并返回此数组. stringObject.split(separator,limit) 我们将按照不同的方式来分割字符串: 使用指定符号分割字符串,代码如 ...

  8. jq 解析josn字符串

    1. var obj = jQuery.parseJSON("${ruleModel.rules}"); 2. var obj = eval("("+" ...

  9. json解析json字符串时候,数组必须对应jsonObjectArray,不能对应JsonObject。否则会解析错误。

    json第三方解析json字符串时候,json数组必须对应jsonObjectArray,不能对应JsonObject.->只要是[]开头的都是json数组字符串,就要用jsonArray解析 ...

随机推荐

  1. 说说eclipse调优,缩短启动时间

    初始配置: -startup plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar --launcher.library plug ...

  2. Firefox mobile (android) and orientationchange

    Firefox for Android does not support the orientationchange event but you can achieve the same result ...

  3. C# WebService调用方法

    public class WebServiceHelper    {        /// < summary>         /// 动态调用web服务         /// < ...

  4. C#用 catch 捕获异类的常用类型

    C#用 catch 捕获异类的常用类型 最近在书上刚刚学到了try和catch用法,然后网上找了下常用的,自己存在这里方便自己查找 Exception 类  描述 SystemException 其他 ...

  5. JS DOM对象控制HTML元素详解

    JS DOM对象控制HTML元素详解 方法: getElementsByName()  获取name getElementsByTagName()  获取元素 getAttribute()  获取元素 ...

  6. Android优化之内存优化倒计时篇

    本文来自网易云社区 作者:聂雷震 本篇文章介绍的内容是如何在安卓手机上实现高效的倒计时效果,这个高效有两个标准:1.刷新频率足够高,让用户觉得这个倒计时的确是倒计时,而不是幻灯片:2.不能占用太多的内 ...

  7. django系列4.2--自定义标签, 自定义过滤器, inclusion_tag, 引入静态文件(css,js等)

    项目的目录为 在app中创建templates时,最好要再创建一个app名相同的文件夹,因为项目找文件时从第一个app开始遍历,不同app内的同名文件会有冲突,所以这样处理 一.自定义标签和过滤器 1 ...

  8. leetcode 74 搜索二维矩阵 java

    题目: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: mat ...

  9. G - Ice_cream's world I (并查集)

    点击打开链接 ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_cream ...

  10. pkuwc 前的任务计划

    菜鸡 wxw 的计划(肯定会咕咕咕 12.27 luogu P4244 [SHOI2008]仙人掌图 II(咕咕咕 luogu P4246 [SHOI2008]堵塞的交通 (没有咕! luogu P1 ...