credential unmarshal added

This commit is contained in:
crusader 2018-08-30 20:12:07 +09:00
parent 7b804b2da2
commit 6288478c89
5 changed files with 91 additions and 8 deletions

View File

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

View File

@ -0,0 +1,69 @@
package credential
import (
"encoding/json"
"testing"
)
func TestCredentialsMap_UnmarshalJSON(t *testing.T) {
credentialsMap := CredentialsMap{
"SNMP": []*SNMPCredential{
&SNMPCredential{
Version: "2c",
Community: "public",
},
&SNMPCredential{
Version: "2c",
Community: "atgame",
},
},
"WINDOWS": []*WindowsCredential{
&WindowsCredential{
DomainName: "atgame.net",
User: "crusader",
Password: "1234",
},
},
}
b, err := json.Marshal(credentialsMap)
if nil != err {
t.Errorf("CredentialsMap.UnmarshalJSON() error = %v", err)
}
_credentialsMap := CredentialsMap{}
type args struct {
data []byte
}
tests := []struct {
name string
m CredentialsMap
args args
wantErr bool
}{
{
name: "1",
m: _credentialsMap,
args: args{
data: b,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.m.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
t.Errorf("CredentialsMap.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
cs := tt.m["SNMP"].([]*SNMPCredential)
for _, c := range cs {
t.Log(c)
}
cs1 := tt.m["WINDOWS"].([]*WindowsCredential)
for _, c1 := range cs1 {
t.Log(c1)
}
})
}
}

View File

@ -4,6 +4,7 @@ import "encoding/json"
type SNMPCredential struct { type SNMPCredential struct {
Credential Credential
Version string `json:"version,omitempty"` Version string `json:"version,omitempty"`
Community string `json:"community,omitempty"` Community string `json:"community,omitempty"`
AuthenticationType string `json:"authenticationType,omitempty"` AuthenticationType string `json:"authenticationType,omitempty"`

View File

@ -4,6 +4,7 @@ import "encoding/json"
type UnixCredential struct { type UnixCredential struct {
Credential Credential
User string `json:"user,omitempty"` User string `json:"user,omitempty"`
LoginType string `json:"loginType,omitempty"` LoginType string `json:"loginType,omitempty"`
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`

View File

@ -2,6 +2,7 @@ package credential
type WindowsCredential struct { type WindowsCredential struct {
Credential Credential
DomainName string `json:"domainName,omitempty"` DomainName string `json:"domainName,omitempty"`
User string `json:"user,omitempty"` User string `json:"user,omitempty"`
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`