mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-19 08:20:05 +08:00
chore: structure support ignore tag
This commit is contained in:
parent
c25a38898f
commit
27b47f976c
@ -53,6 +53,12 @@ func (d *Decoder) Decode(src map[string]any, dst any) error {
|
||||
key, omitKey, found := strings.Cut(tag, ",")
|
||||
omitempty := found && omitKey == "omitempty"
|
||||
|
||||
// As a special case, if the field tag is "-", the field is always omitted.
|
||||
// Note that a field with name "-" can still be generated using the tag "-,".
|
||||
if key == "-" {
|
||||
continue
|
||||
}
|
||||
|
||||
value, ok := src[key]
|
||||
if !ok {
|
||||
if d.option.KeyReplacer != nil {
|
||||
|
||||
@ -288,3 +288,23 @@ func TestStructure_Null(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, s.Opt.Bar, "")
|
||||
}
|
||||
|
||||
func TestStructure_Ignore(t *testing.T) {
|
||||
rawMap := map[string]any{
|
||||
"-": "newData",
|
||||
}
|
||||
|
||||
s := struct {
|
||||
MustIgnore string `test:"-"`
|
||||
}{MustIgnore: "oldData"}
|
||||
|
||||
err := decoder.Decode(rawMap, &s)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, s.MustIgnore, "oldData")
|
||||
|
||||
// test omitempty
|
||||
delete(rawMap, "-")
|
||||
err = decoder.Decode(rawMap, &s)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, s.MustIgnore, "oldData")
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user