diff --git a/config.lisp b/config.lisp index 585f7d7..e102825 100644 --- a/config.lisp +++ b/config.lisp @@ -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)))))) diff --git a/test/test-config.lisp b/test/test-config.lisp index 49979fe..ae31ec1 100644 --- a/test/test-config.lisp +++ b/test/test-config.lisp @@ -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)