preload json values in structs to determine defaults

This commit is contained in:
tarat44 2021-10-10 17:52:26 -04:00
parent 3fe637156c
commit 166269f93b
2 changed files with 24 additions and 18 deletions

View File

@ -76,9 +76,17 @@ func (t *CheckDefinition) UnmarshalJSON(data []byte) (err error) {
}{ }{
Alias: (*Alias)(t), Alias: (*Alias)(t),
} }
// Set default values
// Preevaluate struct values to determine where to set defaults
if err = lib.UnmarshalJSON(data, &aux); err != nil {
return err
}
// Set defaults
if aux.H2PING != "" {
aux.H2PingUseTLS = true aux.H2PingUseTLS = true
aux.H2PingUseTLSSnake = true aux.H2PingUseTLSSnake = true
}
if err = lib.UnmarshalJSON(data, &aux); err != nil { if err = lib.UnmarshalJSON(data, &aux); err != nil {
return err return err
} }
@ -108,14 +116,10 @@ func (t *CheckDefinition) UnmarshalJSON(data []byte) (err error) {
if t.ServiceID == "" { if t.ServiceID == "" {
t.ServiceID = aux.ServiceIDSnake t.ServiceID = aux.ServiceIDSnake
} }
if !aux.H2PingUseTLSSnake {
if (aux.H2PING != "" && !aux.H2PingUseTLSSnake) || (aux.H2PING == "" && aux.H2PingUseTLSSnake) {
t.H2PingUseTLS = aux.H2PingUseTLSSnake t.H2PingUseTLS = aux.H2PingUseTLSSnake
} }
// unset default values if it is not an H2Ping check
if t.H2PING == "" {
aux.H2PingUseTLS = false
aux.H2PingUseTLSSnake = false
}
// Parse special values // Parse special values
if aux.Interval != nil { if aux.Interval != nil {

View File

@ -93,9 +93,16 @@ func (t *CheckType) UnmarshalJSON(data []byte) (err error) {
}{ }{
Alias: (*Alias)(t), Alias: (*Alias)(t),
} }
// set default values
// Preevaluate struct values to determine where to set defaults
if err = lib.UnmarshalJSON(data, aux); err != nil {
return err
}
// Set defaults
if aux.H2PING != "" {
aux.H2PingUseTLS = true aux.H2PingUseTLS = true
aux.H2PingUseTLSSnake = true aux.H2PingUseTLSSnake = true
}
if err = lib.UnmarshalJSON(data, aux); err != nil { if err = lib.UnmarshalJSON(data, aux); err != nil {
return err return err
@ -161,14 +168,9 @@ func (t *CheckType) UnmarshalJSON(data []byte) (err error) {
t.DeregisterCriticalServiceAfter = time.Duration(v) t.DeregisterCriticalServiceAfter = time.Duration(v)
} }
} }
if !aux.H2PingUseTLSSnake { if (aux.H2PING != "" && !aux.H2PingUseTLSSnake) || (aux.H2PING == "" && aux.H2PingUseTLSSnake) {
t.H2PingUseTLS = aux.H2PingUseTLSSnake t.H2PingUseTLS = aux.H2PingUseTLSSnake
} }
// unset default values if it is not an H2Ping check
if t.H2PING == "" {
aux.H2PingUseTLS = false
aux.H2PingUseTLSSnake = false
}
return nil return nil