登录
首页 >  Golang >  Go问答

如何使用 r3labs/diff 比较整个结构

来源:stackoverflow

时间:2024-04-01 17:54:32 154浏览 收藏

哈喽!今天心血来潮给大家带来了《如何使用 r3labs/diff 比较整个结构》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我遇到了 go 语言的 github.com/r3labs/diff 库来比较相同类型的两个结构。

库运行得很好,除了以下一个用例:我使用 date 结构来表示日期:

type date struct {
    year  int
    month int
    day   int
}

现在,还有一些其他更复杂的结构使用 date 结构,例如:

type student struct {
  dateofbirth date
}

如果我要比较两个学生,比如

diff.Diff(
  Student{DateOfBirth: Date{2021, 11, 13}},
  Student{DateOfBirth: Date{2021, 10, 9}},
)

我得到的结果是包含 2 项的变更日志,一项用于 dateofbirth > month,另一项用于 dateofbirth > day

我想要的结果是包含单个项目 (dateofbirth) 和 2021-10-09 值的变更日志。

图书馆有可能做到这一点吗?


正确答案


注意此解决方案使用 github.com/r3labs/diff/v2

没有这样的选项。 diff 只是递归地处理结构字段并为每个不同的字段生成更改日志。

要实现您想要的输出,您可以实现自己的 ValueDiffer。这样您就可以“原子地”比较结构并以您想要的格式附加到变更日志。

一个人为的示例,部分从包内部复制:

type datediffer struct {
}

// whether this differ should be used to match a specific type
func (d *datediffer) match(a, b reflect.value) bool {
    return diff.aretype(a, b, reflect.typeof(date{}))
}

// the actual diff function, where you also append to the changelog
// using your custom format
func (d *datediffer) diff(cl *diff.changelog, path []string, a, b reflect.value) error {
    if a.kind() == reflect.invalid {
        cl.add(diff.create, path, nil, b.interface())
        return nil
    }
    if b.kind() == reflect.invalid {
        cl.add(diff.delete, path, a.interface(), nil)
        return nil
    }
    var d1, d2 date
    d1, _ = a.interface().(date)
    d2, _ = b.interface().(date)
    if d1.day != d2.day || d1.month != d2.month || d1.year != d2.year {
        cl.add(diff.update, path, fmt.sprintf("%d-%d-%d", d1.year, d1.month, d1.day), fmt.sprintf("%d-%d-%d", d2.year, d2.month, d2.day))
    }
    return nil
}

// unsure what this is actually for, but you must implement it either way
func (d *datediffer) insertparentdiffer(dfunc func(path []string, a, b reflect.value, p interface{}) error) {
    return
}

然后你这样使用它:

d2, _ := diff.newdiffer(diff.customvaluediffers(&datediffer{}))

    s1 := student{dateofbirth: date{2021, 11, 13}}
    s2 := student{dateofbirth: date{2021, 10, 9}}

    ch2, _ := d2.diff(s1, s2)

输出(json编组和缩进):

[
  {
   "type": "update",
   "path": [
    "dateofbirth"
   ],
   "from": "2021-11-13",
   "to": "2021-10-9"
  }
 ]

经过一番研究,我找到了解决方案。

我需要为 date 创建自定义差异,并使用包中的 disablestructvalues 选项。

此选项很有用,因为它禁止为结构中的每个项目填充单独的更改,并在将其与 nil 值进行比较时返回整个对象。

diff.diff(
  student{dateofbirth: date{2021, 11, 13}},
  student{dateofbirth: date{2021, 10, 9}},
  diff.customvaluediffers(differ.datediffer{}),
  diff.disablestructvalues()
)

要实现自定义差异,需要一个实现以下接口的新结构:

type valuediffer interface {
    match(a, b reflect.value) bool
    diff(cl *changelog, path []string, a, b reflect.value) error
    insertparentdiffer(dfunc func(path []string, a, b reflect.value, p interface{}) error)
}

这是我的自定义差异的实现。

type DateDiffer struct {
    DiffFunc (func(path []string, a, b reflect.Value, p interface{}) error)
}

func (differ DateDiffer) Match(a, b reflect.Value) bool {
    return diff.AreType(a, b, reflect.TypeOf(Date{}))
}

func (differ DateDiffer) Diff(cl *diff.Changelog, path []string, a, b reflect.Value) error {
    if a.Kind() == reflect.Invalid {
        cl.Add(diff.CREATE, path, nil, b.Interface())
        return nil
    }

    if b.Kind() == reflect.Invalid {
        cl.Add(diff.DELETE, path, a.Interface(), nil)
        return nil
    }

    var source, target Date
    source, _ = a.Interface().(Date)
    target, _ = b.Interface().(Date)
    if !source.Equal(target) {
        cl.Add(diff.UPDATE, path, a.Interface(), b.Interface())
    }

    return nil
}

func (differ DateDiffer) InsertParentDiffer(dfunc func(path []string, a, b reflect.Value, p interface{}) error) {
    differ.DiffFunc = dfunc
}

希望这会对有类似用例的人有所帮助。

今天关于《如何使用 r3labs/diff 比较整个结构》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>