├── cl-config.asd ├── README └── package.lisp /cl-config.asd: -------------------------------------------------------------------------------- 1 | ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*- 2 | (defpackage #:cl-config-asd 3 | (:use :cl :asdf)) 4 | 5 | (in-package :cl-config-asd) 6 | 7 | (defsystem cl-config 8 | :name "Cl config" 9 | :version "0.1.0" 10 | :author "Olexiy Zamkoviy" 11 | :licence "Public Domain" 12 | :description "Simple configuration library" 13 | :depends-on () 14 | :components ((:file "package"))) 15 | 16 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | 2 | cl-config is a small configuration library. 3 | 4 | It's main difference from usual configuration functionality is that it uses function return values as parameters so configuration can be dynamical. 5 | 6 | You set function as configuration value, when you want to get this configuration value function will be called in specific context and it's return value will be used as configuration value. 7 | 8 | For example 9 | 10 | (cl-config:set-value 11 | :some-box-width 12 | (lambda (box-side) 13 | (if (equal box-side :a) 14 | 10 15 | 20))) 16 | ; key key context 17 | 18 | (cl-config:get-value :some-box-width :a) 19 | ; 10 20 | (cl-config:get-value :some-box-width :b) 21 | ; 20 22 | 23 | If you want to use function itself as config value just use :functionp parameter. 24 | (cl-config:set-value 25 | :some-box-width-function 26 | (lambda (box-side) 123) 27 | :functionp t) 28 | 29 | (cl-config:get-value :some-box-width-function) 30 | ; # 31 | 32 | Other configuration values set as usual 33 | 34 | (cl-config:set-value :some-key "Some value") 35 | 36 | (cl-config:get-value :some-key) 37 | ; "Some value" 38 | 39 | And remember that you can use some kind of namespaces with keywords 40 | 41 | (cl-config:set-value :some-package.complex-key "Some value") 42 | 43 | (cl-config:get-value :some-package.complex-key) 44 | ; "Some value" 45 | 46 | 47 | -------------------------------------------------------------------------------- /package.lisp: -------------------------------------------------------------------------------- 1 | 2 | (defpackage #:cl-config 3 | (:use :cl) 4 | (:documentation 5 | "Simple configuration library") 6 | (:export #:get-value #:set-value)) 7 | 8 | (in-package :cl-config) 9 | 10 | (defvar *config-values* nil) 11 | 12 | (defclass config-callback-value () 13 | ((function :initarg :function))) 14 | 15 | (defun set-value (key value &key functionp) 16 | (setf (getf *config-values* key) 17 | (if functionp 18 | (progn 19 | (assert (functionp value)) 20 | (make-instance 'config-callback-value :function value)) 21 | value))) 22 | 23 | (defun get-value (key &rest context) 24 | (let ((default-value (and 25 | (evenp (length context)) 26 | (find :default context) 27 | (getf context :default))) 28 | (value (getf *config-values* key))) 29 | (cond 30 | ((functionp value) 31 | (apply value context)) 32 | ((equal 'config-callback-value 33 | (type-of value)) 34 | (slot-value value 'function)) 35 | (t (or value default-value))))) 36 | 37 | (defun test-sets-value () 38 | (declare (special *config-values*)) 39 | (set-value :test1 "Test 1") 40 | (assert (string= (getf *config-values* :test1) "Test 1"))) 41 | 42 | (defun test-sets-function-value () 43 | (declare (special *config-values*)) 44 | (set-value :test2 #'test-sets-function-value :functionp t) 45 | (assert (equal #'test-sets-function-value (get-value :test2)))) 46 | 47 | (defun test-sets-context-value () 48 | (set-value :test3 49 | (lambda (number) 50 | (if (= number 1) 51 | "1" 52 | "2"))) 53 | (assert (string= (get-value :test3 1) "1")) 54 | (assert (string= (get-value :test3 2) "2"))) 55 | 56 | (defun test-returns-default-value () 57 | (assert (= 1 (get-value :test4 :default 1)))) 58 | 59 | (defun test-returns-overrided-value-if-default-is-set () 60 | (set-value :test5 2) 61 | (assert (= 2 (get-value :test5 :default 1)))) 62 | 63 | (defun test-all () 64 | (declare (special *config-values*)) 65 | (test-sets-value) 66 | (test-sets-function-value) 67 | (test-sets-context-value) 68 | (test-returns-default-value) 69 | (test-returns-overrided-value-if-default-is-set) 70 | (setf *config-values* nil)) 71 | 72 | (test-all) 73 | --------------------------------------------------------------------------------