work in progress: config items hierarchy (root, children, parent, ...)

This commit is contained in:
Helmut Merz 2024-06-07 16:34:58 +02:00
parent 5a63a1034d
commit 81043d0e35
3 changed files with 30 additions and 6 deletions

View file

@ -4,12 +4,22 @@
(defpackage :scopes/config (defpackage :scopes/config
(:use :common-lisp) (:use :common-lisp)
(:export #:base (:export #:base #:root
#:env-data #:env-keys #:env-prefix #:env-path #:hash-to-slots)) #:env-data #:env-keys #:env-prefix #:env-path
#:add #:children #:parent
#:hash-to-slots))
(in-package :scopes/config) (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 ((env-keys :reader env-keys
:initform nil :initform nil
:allocation :class) :allocation :class)
@ -22,7 +32,7 @@
(env-data :accessor env-data (env-data :accessor env-data
:initform (make-hash-table)))) :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)) (let* ((data (env-data cfg))
(prefix (env-prefix cfg)) (prefix (env-prefix cfg))
(ep (env-path cfg)) (ep (env-path cfg))
@ -35,6 +45,20 @@
(setf (gethash sl data) env-val) (setf (gethash sl data) env-val)
(setf (gethash sl data) dotenv-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) (defun hash-to-slots (ht obj slots)
(if ht (if ht
(dolist (sl slots) (dolist (sl slots)

View file

@ -9,7 +9,7 @@
(in-package :scopes/core) (in-package :scopes/core)
(defclass config (config:base) (defclass config (config:root)
(services)) (services))
(defclass message () (defclass message ()

View file

@ -14,7 +14,7 @@
(defvar *config* nil) (defvar *config* nil)
(defclass test-config (config:base) (defclass test-config (config:root)
((config:env-keys :initform '(:user :password)) ((config:env-keys :initform '(:user :password))
(config:env-path :initform (t:test-path ".test.env")) (config:env-path :initform (t:test-path ".test.env"))
(user :accessor user :initarg :user) (user :accessor user :initarg :user)