move update of slot values to separate function

This commit is contained in:
Helmut Merz 2024-06-07 14:00:20 +02:00
parent d9819cbe2e
commit 5a63a1034d
2 changed files with 10 additions and 6 deletions

View file

@ -5,7 +5,7 @@
(defpackage :scopes/config
(:use :common-lisp)
(:export #:base
#:env-data #:env-keys #:env-prefix #:env-path))
#:env-data #:env-keys #:env-prefix #:env-path #:hash-to-slots))
(in-package :scopes/config)
@ -34,3 +34,11 @@
(if env-val
(setf (gethash sl data) env-val)
(setf (gethash sl data) dotenv-val))))))
(defun hash-to-slots (ht obj slots)
(if ht
(dolist (sl slots)
(let* ((key (intern (string sl) :keyword))
(val (gethash key ht)))
(if val
(setf (slot-value obj sl) val))))))

View file

@ -21,11 +21,7 @@
(password :accessor password :initarg :password)))
(defmethod initialize-instance :after ((cfg test-config) &key &allow-other-keys)
(let* ((data (config:env-data cfg))
(u (if data (gethash :user data)))
(pw (if data (gethash :password data))))
(if u (setf (user cfg) u))
(if pw (setf (password cfg) pw))))
(config:hash-to-slots (config:env-data cfg) cfg '(user password)))
(defun run ()
(let ((*config* nil)