@@ -40,6 +40,25 @@ type CasbinRule struct {
4040 V5 string
4141}
4242
43+ // Config represents the configuration for the Redis adapter.
44+ type Config struct {
45+ // Network is the network type, e.g., "tcp", "unix"
46+ Network string
47+ // Address is the Redis server address, e.g., "127.0.0.1:6379"
48+ Address string
49+ // Key is the Redis key to store Casbin rules (default: "casbin_rules")
50+ Key string
51+ // Username for Redis authentication (optional)
52+ Username string
53+ // Password for Redis authentication (optional)
54+ Password string
55+ // TLSConfig for secure connections (optional)
56+ TLSConfig * tls.Config
57+ // Pool is an existing Redis connection pool (optional)
58+ // If provided, Network, Address, Username, Password, and TLSConfig are ignored
59+ Pool * redis.Pool
60+ }
61+
4362// Adapter represents the Redis adapter for policy storage.
4463type Adapter struct {
4564 network string
@@ -78,94 +97,148 @@ func finalizer(a *Adapter) {
7897 }
7998}
8099
81- func newAdapter (network string , address string , key string ,
82- username string , password string ) (* Adapter , error ) {
100+ // NewAdapter creates a new Redis adapter with the provided configuration.
101+ func NewAdapter (config * Config ) (* Adapter , error ) {
102+ if config == nil {
103+ return nil , errors .New ("config cannot be nil" )
104+ }
105+
83106 a := & Adapter {}
84- a .network = network
85- a .address = address
86- a .key = key
87- a .username = username
88- a .password = password
89107
90- // Open the DB, create it if not existed.
91- err := a .open ()
108+ // Set default key if not provided
109+ if config .Key == "" {
110+ a .key = "casbin_rules"
111+ } else {
112+ a .key = config .Key
113+ }
114+
115+ // If a pool is provided, use it
116+ if config .Pool != nil {
117+ a ._pool = config .Pool
118+ } else {
119+ // Otherwise, create a new connection
120+ if config .Network == "" {
121+ return nil , errors .New ("network is required when not using a pool" )
122+ }
123+ if config .Address == "" {
124+ return nil , errors .New ("address is required when not using a pool" )
125+ }
126+
127+ a .network = config .Network
128+ a .address = config .Address
129+ a .username = config .Username
130+ a .password = config .Password
131+ a .tlsConfig = config .TLSConfig
132+
133+ // Open the DB connection
134+ err := a .open ()
135+ if err != nil {
136+ return nil , err
137+ }
138+ }
92139
93140 // Call the destructor when the object is released.
94141 runtime .SetFinalizer (a , finalizer )
95142
96- return a , err
143+ return a , nil
97144}
98145
99- // NewAdapter is the constructor for Adapter.
100- func NewAdapter (network string , address string ) (* Adapter , error ) {
101- return newAdapter (network , address , "casbin_rules" , "" , "" )
146+ // Legacy constructor functions (deprecated)
147+ // These are kept for backward compatibility but should be avoided in new code
148+
149+ // NewAdapterBasic is the basic constructor for Adapter.
150+ // Deprecated: Use NewAdapter with Config struct instead.
151+ func NewAdapterBasic (network string , address string ) (* Adapter , error ) {
152+ config := & Config {
153+ Network : network ,
154+ Address : address ,
155+ }
156+ return NewAdapter (config )
102157}
103158
159+ // NewAdapterWithUser creates adapter with user credentials.
160+ // Deprecated: Use NewAdapter with Config struct instead.
104161func NewAdapterWithUser (network string , address string , username string , password string ) (* Adapter , error ) {
105- return newAdapter (network , address , "casbin_rules" , username , password )
162+ config := & Config {
163+ Network : network ,
164+ Address : address ,
165+ Username : username ,
166+ Password : password ,
167+ }
168+ return NewAdapter (config )
106169}
107170
108- // NewAdapterWithPassword is the constructor for Adapter.
171+ // NewAdapterWithPassword creates adapter with password authentication.
172+ // Deprecated: Use NewAdapter with Config struct instead.
109173func NewAdapterWithPassword (network string , address string , password string ) (* Adapter , error ) {
110- return newAdapter (network , address , "casbin_rules" , "" , password )
174+ config := & Config {
175+ Network : network ,
176+ Address : address ,
177+ Password : password ,
178+ }
179+ return NewAdapter (config )
111180}
112181
113- // NewAdapterWithKey is the constructor for Adapter.
182+ // NewAdapterWithKey creates adapter with custom key.
183+ // Deprecated: Use NewAdapter with Config struct instead.
114184func NewAdapterWithKey (network string , address string , key string ) (* Adapter , error ) {
115- return newAdapter (network , address , key , "" , "" )
185+ config := & Config {
186+ Network : network ,
187+ Address : address ,
188+ Key : key ,
189+ }
190+ return NewAdapter (config )
116191}
117192
118- // NewAdapterWithPool is the constructor for Adapter.
193+ // NewAdapterWithPool creates adapter with connection pool.
194+ // Deprecated: Use NewAdapter with Config struct instead.
119195func NewAdapterWithPool (pool * redis.Pool ) (* Adapter , error ) {
120- a := & Adapter {}
121- a .key = "casbin_rules"
122-
123- conn := pool .Get ()
124- defer a .release (conn )
125-
126- a ._conn = conn
127- a ._pool = pool
128-
129- // Call the destructor when the object is released.
130- runtime .SetFinalizer (a , finalizer )
131-
132- return a , nil
196+ config := & Config {
197+ Pool : pool ,
198+ }
199+ return NewAdapter (config )
133200}
134201
135- // NewAdapterWithPoolAndOptions is the constructor for Adapter.
202+ // NewAdapterWithPoolAndOptions creates adapter with pool and options.
203+ // Deprecated: Use NewAdapter with Config struct instead.
136204func NewAdapterWithPoolAndOptions (pool * redis.Pool , options ... Option ) (* Adapter , error ) {
137- a := & Adapter {}
138- a .key = "casbin_rules"
205+ config := & Config {
206+ Pool : pool ,
207+ }
208+ a , err := NewAdapter (config )
209+ if err != nil {
210+ return nil , err
211+ }
212+
213+ // Apply options for backward compatibility
139214 for _ , option := range options {
140215 option (a )
141216 }
142217
143- conn := pool .Get ()
144- defer a .release (conn )
145-
146- a ._conn = conn
147- a ._pool = pool
148-
149- // Call the destructor when the object is released.
150- runtime .SetFinalizer (a , finalizer )
151-
152218 return a , nil
153219}
154220
155221type Option func (* Adapter )
156222
223+ // NewAdapterWithOption creates adapter with options pattern.
224+ // Deprecated: Use NewAdapter with Config struct instead.
157225func NewAdapterWithOption (options ... Option ) (* Adapter , error ) {
158226 a := & Adapter {}
159227 for _ , option := range options {
160228 option (a )
161229 }
162- // Open the DB, create it if not existed.
163- err := a .open ()
164230
165- // Call the destructor when the object is released.
166- runtime .SetFinalizer (a , finalizer )
231+ // Convert to new config-based approach
232+ config := & Config {
233+ Network : a .network ,
234+ Address : a .address ,
235+ Key : a .key ,
236+ Username : a .username ,
237+ Password : a .password ,
238+ TLSConfig : a .tlsConfig ,
239+ }
167240
168- return a , err
241+ return NewAdapter ( config )
169242}
170243
171244func WithAddress (address string ) Option {
@@ -191,6 +264,7 @@ func WithNetwork(network string) Option {
191264 a .network = network
192265 }
193266}
267+
194268func WithKey (key string ) Option {
195269 return func (a * Adapter ) {
196270 a .key = key
0 commit comments