Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dda154a

Browse files
authored
Merge pull request #3370 from gophercloud/user-bool
identity: add support for string boolean in users' enabled member
2 parents ad0459b + d451208 commit dda154a

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

openstack/identity/v3/users/results.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package users
22

33
import (
44
"encoding/json"
5+
"fmt"
6+
"strconv"
57
"time"
68

79
"github.com/gophercloud/gophercloud/v2"
@@ -20,7 +22,7 @@ type User struct {
2022
DomainID string `json:"domain_id"`
2123

2224
// Enabled is whether or not the user is enabled.
23-
Enabled bool `json:"enabled"`
25+
Enabled bool `json:"-"`
2426

2527
// Extra is a collection of miscellaneous key/values.
2628
Extra map[string]any `json:"-"`
@@ -45,6 +47,7 @@ func (r *User) UnmarshalJSON(b []byte) error {
4547
type tmp User
4648
var s struct {
4749
tmp
50+
Enabled any `json:"enabled"`
4851
Extra map[string]any `json:"extra"`
4952
PasswordExpiresAt gophercloud.JSONRFC3339MilliNoZ `json:"password_expires_at"`
5053
}
@@ -56,6 +59,20 @@ func (r *User) UnmarshalJSON(b []byte) error {
5659

5760
r.PasswordExpiresAt = time.Time(s.PasswordExpiresAt)
5861

62+
switch t := s.Enabled.(type) {
63+
case nil:
64+
r.Enabled = false
65+
case bool:
66+
r.Enabled = t
67+
case string:
68+
r.Enabled, err = strconv.ParseBool(t)
69+
if err != nil {
70+
return fmt.Errorf("Failed to parse Enabled %q: %v", t, err)
71+
}
72+
default:
73+
return fmt.Errorf("Unknown type for Enabled: %T (value: %v)", t, t)
74+
}
75+
5976
// Collect other fields and bundle them into Extra
6077
// but only if a field titled "extra" wasn't sent.
6178
if s.Extra != nil {

openstack/identity/v3/users/testing/fixtures_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const ListOutput = `
2525
"users": [
2626
{
2727
"domain_id": "default",
28-
"enabled": true,
28+
"enabled": "True",
2929
"id": "2844b2a08be147a08ef58317d6471f1f",
3030
"links": {
3131
"self": "http://example.com/identity/v3/users/2844b2a08be147a08ef58317d6471f1f"

0 commit comments

Comments
 (0)