Skip to content

v0.6.0

Compare
Choose a tag to compare
@ggicci ggicci released this 13 Nov 16:48
· 16 commits to main since this release

New Features

Sort directives at tree build time and resolving/scanning runtime:

type Record struct {
	R1 string `owl:"DOTA=2;csgo=1"`
	R2 string `owl:"apple=green;pear;Grape=purple"`
}

1. When passing the option to New:

owl.New(Record{}, owl.WithDirectiveRunOrder(func(d1, d2 *owl.Directive) bool {
    return strings.ToLower(d1.Name) < strings.ToLower(d2.Name) // sort directives by name (alphabetical order)
}))

the directives will be sorted at tree build stage. I.e. The Resolver built for field Record.R2 looks like this:

Resolver(R2).Directives ==> []*Directive {
    &Directive{Name: "apple", Argv: { "green" }},
    &Directive{Name: "Grape", Argv: { "purple" }},
    &Directive{Name: "apple", Argv: {}},
}

Actually we can use the API Resolver.Iterate to achieve the same result.

2. When passing the option to Resolve or Scan:

resolver.Scan(form, owl.WithDirectiveRunOrder(func(d1, d2 *owl.Directive) bool {
    return d1.Name == "default" // makes default directive run first
}))

the original directives order won't be affected. A copy of the directives will be created an sorted.