From 81043d0e35d360327cfba3108538a5d42bf64d35 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Fri, 7 Jun 2024 16:34:58 +0200 Subject: [PATCH] work in progress: config items hierarchy (root, children, parent, ...) --- config.lisp | 32 ++++++++++++++++++++++++++++---- core/core.lisp | 2 +- test/test-config.lisp | 2 +- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/config.lisp b/config.lisp index e102825..1846b2d 100644 --- a/config.lisp +++ b/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) diff --git a/core/core.lisp b/core/core.lisp index 6589129..bb87461 100644 --- a/core/core.lisp +++ b/core/core.lisp @@ -9,7 +9,7 @@ (in-package :scopes/core) -(defclass config (config:base) +(defclass config (config:root) (services)) (defclass message () diff --git a/test/test-config.lisp b/test/test-config.lisp index ae31ec1..7696a89 100644 --- a/test/test-config.lisp +++ b/test/test-config.lisp @@ -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)