Skip to content

Commit

Permalink
simpify the configuration parsing (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander authored Jul 31, 2023
1 parent 2805e2f commit 9629c8d
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type config struct {
users map[string]string
}

type rawConfig struct {
Users []User
}

type User struct {
Username string
Password string
Expand All @@ -34,22 +38,20 @@ func (p *parser) Parse(any *anypb.Any) (interface{}, error) {

v := configStruct.Value
conf := &config{}
var users []User
rc := &rawConfig{}

if userMap, ok := v.AsMap()["users"]; ok {
data, err := json.Marshal(userMap)
if err != nil {
return conf, err
}

err = json.Unmarshal(data, &users)
if err != nil {
return conf, err
}
data, err := v.MarshalJSON()
if err != nil {
return nil, err
}

conf.users = paresUser2Map(&users)
err = json.Unmarshal(data, rc)
if err != nil {
return nil, err
}

conf.users = paresUser2Map(&rc.Users)

return conf, nil
}

Expand Down

0 comments on commit 9629c8d

Please sign in to comment.