model/config/credential/Credential.go

56 lines
1.1 KiB
Go
Raw Permalink Normal View History

2018-08-30 09:57:59 +00:00
package credential
import (
"encoding/json"
"errors"
2018-08-30 11:12:07 +00:00
"reflect"
2018-08-30 09:57:59 +00:00
"git.loafle.net/overflow/model/meta"
)
type Credential struct {
}
2018-08-30 11:12:07 +00:00
// CredentialsMap is map of array of Credential
type CredentialsMap map[string]interface{}
2018-08-30 09:57:59 +00:00
2018-08-30 11:12:07 +00:00
func (m *CredentialsMap) UnmarshalJSON(data []byte) error {
2018-08-30 09:57:59 +00:00
credentials := make(map[string]json.RawMessage)
err := json.Unmarshal(data, &credentials)
if nil != err {
return err
}
2018-08-30 11:12:07 +00:00
result := make(CredentialsMap)
2018-08-30 09:57:59 +00:00
for k, v := range credentials {
var i interface{}
switch meta.ToMetaCredentialTypeEnum(&meta.MetaCredentialType{Key: k}) {
case meta.MetaCredentialTypeEnumSNMP:
2018-08-30 11:12:07 +00:00
var _i []*SNMPCredential
i = &_i
2018-08-30 09:57:59 +00:00
case meta.MetaCredentialTypeEnumWindows:
2018-08-30 11:12:07 +00:00
var _i []*WindowsCredential
i = &_i
2018-08-30 09:57:59 +00:00
default:
return errors.New("Unrecognized Credential")
}
2018-08-30 11:12:07 +00:00
err := json.Unmarshal(v, &i)
2018-08-30 09:57:59 +00:00
if nil != err {
return err
}
2018-08-30 11:12:07 +00:00
result[k] = reflect.ValueOf(i).Elem().Interface()
2018-08-30 09:57:59 +00:00
}
*m = result
return nil
}
2018-08-30 11:12:07 +00:00
func unmarshalJSON(v json.RawMessage, i interface{}) error {
err := json.Unmarshal(v, &i)
if nil != err {
return err
}
return nil
}