work in progress: config items hierarchy (root, children, parent, ...)
This commit is contained in:
parent
5a63a1034d
commit
81043d0e35
3 changed files with 30 additions and 6 deletions
32
config.lisp
32
config.lisp
|
@ -4,12 +4,22 @@
|
|||
|
||||
(defpackage :scopes/config
|
||||
(:use :common-lisp)
|
||||
(:export #:base
|
||||
#:env-data #:env-keys #:env-prefix #:env-path #:hash-to-slots))
|
||||
(:export #:base #:root
|
||||
#:env-data #:env-keys #:env-prefix #:env-path
|
||||
#:add #:children #:parent
|
||||
#:hash-to-slots))
|
||||
|
||||
(in-package :scopes/config)
|
||||
|
||||
(defclass base ()
|
||||
(defclass common ()
|
||||
((children :accessor children
|
||||
:initarg children
|
||||
:initform nil)))
|
||||
|
||||
(defgeneric parent (cfg)
|
||||
(:method ((cfg common)) nil))
|
||||
|
||||
(defclass root (common)
|
||||
((env-keys :reader env-keys
|
||||
:initform nil
|
||||
:allocation :class)
|
||||
|
@ -22,7 +32,7 @@
|
|||
(env-data :accessor env-data
|
||||
:initform (make-hash-table))))
|
||||
|
||||
(defmethod initialize-instance :after ((cfg base) &key &allow-other-keys)
|
||||
(defmethod initialize-instance :after ((cfg root) &key &allow-other-keys)
|
||||
(let* ((data (env-data cfg))
|
||||
(prefix (env-prefix cfg))
|
||||
(ep (env-path cfg))
|
||||
|
@ -35,6 +45,20 @@
|
|||
(setf (gethash sl data) env-val)
|
||||
(setf (gethash sl data) dotenv-val))))))
|
||||
|
||||
(defclass base (common)
|
||||
((parent :reader parent
|
||||
:initarg :parent)))
|
||||
|
||||
(defgeneric add (cfg child)
|
||||
(:method ((cfg common) (child base))
|
||||
(push (children cfg) child)
|
||||
(setf (parent child) cfg)))
|
||||
|
||||
(defmethod env-data ((cfg base))
|
||||
(env-data (parent (cfg))))
|
||||
|
||||
;;;; utility functions
|
||||
|
||||
(defun hash-to-slots (ht obj slots)
|
||||
(if ht
|
||||
(dolist (sl slots)
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
(in-package :scopes/core)
|
||||
|
||||
(defclass config (config:base)
|
||||
(defclass config (config:root)
|
||||
(services))
|
||||
|
||||
(defclass message ()
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
(defvar *config* nil)
|
||||
|
||||
(defclass test-config (config:base)
|
||||
(defclass test-config (config:root)
|
||||
((config:env-keys :initform '(:user :password))
|
||||
(config:env-path :initform (t:test-path ".test.env"))
|
||||
(user :accessor user :initarg :user)
|
||||
|
|
Loading…
Add table
Reference in a new issue