unofficial mirror of help-guix@gnu.org 
 help / color / mirror / Atom feed
* Help on writing config serialiser
@ 2022-01-24 16:13 Simon Streit
  2022-01-28 13:13 ` Is the example about the serialiser in the manual correct? Was: " Simon Streit
  0 siblings, 1 reply; 2+ messages in thread
From: Simon Streit @ 2022-01-24 16:13 UTC (permalink / raw)
  To: help-guix

Hello!

I am trying to write a config serialiser for Samba's smb.conf.  While
developing the service, I noticed that my current approach should be
done in while using a config serialiser.  I modified the example that
is in the manual:
--8<---------------cut here---------------start------------->8---
(use-modules (gnu services)
             (guix gexp)
             (gnu services configuration)
             (srfi srfi-26)
             (srfi srfi-1))

;; Turn field names, which are Scheme symbols into strings
(define (uglify-field-name field-name)
  (let ((str (symbol->string field-name)))
    ;; field? -> is-field
    (if (string-suffix? "?" str)
        (string-append "is-" (string-drop-right str 1))
        str)))

(define (serialize-string field-name value)
  #~(string-append #$(uglify-field-name field-name) " = " #$value "\n"))

(define (serialize-integer field-name value)
  (serialize-string field-name (number->string value)))

(define (serialize-boolean field-name value)
  (serialize-string field-name (if value "yes" "no")))

;;; [sections]
(define (serialize-section-name field-name value)
  #~(string-append "\n[" #$value "]\n"))

(define (list-of-smb-conf-sections? lst) ;wrong type to apply?!?
  (every smb-conf-section? lst))

(define (serialize-list-of-smb-conf-sections field-name value)
  #~(string-append #$@(map (cut serialize-configuration <>
                                smb-conf-section-fields)
                           value)))

;;; [global]
(define (serialize-smb-conf configuration)
  (mixed-text-file
   "smb.conf"
   #~(string-append ;; "[global]\n"
                    #$(serialize-configuration
                       configuration smb-conf-fields))))

(define-maybe integer)
(define-maybe string)


;;; section part

(define-configuration smb-conf-section
  ;; (package
  ;;   (file-like samba)
  ;;   "The @var{samba} package.")

  (section-name
   (string)
   "Name of section that describes a shared resource, also known as
``share''.  The section name is the name of the shared resource and
the parameters withing the section define the shares attributes."
   serialize-section-name)

  ;; From here on all parameter options described in smb.conf(5)
  ;; should comes below.  Currently this list is incomplete, needs
  ;; testing, and it would be nice to have a converter to easily
  ;; convert the parameters directly found in Samba's sources.

  ;; For instance, the manpage for smb.conf is automatically
  ;; generated.  It should be possible to extract this from source,
  ;; and normalise it here.

  (workgroup                            ;(G)
   ;; (string "WORKGROUP")
   (maybe-string "WORKGROUP")
   "This controls what workgroup your server will appear to be in when
queried by clients.  Note that this parameter also controls the Domain
name used with the @code{security = domain} setting.

Default: @code{(\"WORKGROUP\")}

Example: @code{(\"MYGROUP\")}"))

;;; main part

(define-configuration smb-conf
  (workgroup
   (string "WORKGROUP")
   "")

  (sections
   (list-of-smb-conf-sections '())
   "List of sections are generated here."))

(define my-smb-conf
  (smb-conf
   ;; (workgroup "GNU")
   (sections
    (list
     (smb-conf-section
      (section-name "global")
      (workgroup "GNU")
      )))
   ))
--8<---------------cut here---------------end--------------->8---

Unfortunately, this procedure fails with an error:
--8<---------------cut here---------------start------------->8---
ice-9/boot-9.scm:1685:16: In procedure raise-exception:
In procedure every: Wrong type argument: #<syntax-transformer smb-conf-section?>
--8<---------------cut here---------------end--------------->8---

And this error is not clear to me yet.  I've been stuck at comparing the
example with my version.  I'd be happy to receive a bit of help. 


Kind regards
Simon 


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Is the example about the serialiser in the manual correct? Was: Help on writing config serialiser
  2022-01-24 16:13 Help on writing config serialiser Simon Streit
@ 2022-01-28 13:13 ` Simon Streit
  0 siblings, 0 replies; 2+ messages in thread
From: Simon Streit @ 2022-01-28 13:13 UTC (permalink / raw)
  To: help-guix

Hello,

I gave up, and thought of starting from scratch.  But my initial attempt
failed right from the beginning.

I'm not so sure now if the example provided in the manual is usable in
its current state.  The following is a (slightly) unedited copy from the
manual:

--8<---------------cut here---------------start------------->8---
(use-modules (gnu services)
             (guix gexp)
             (gnu services configuration)
             (srfi srfi-26)
             (srfi srfi-1))

;; Turn field names, which are Scheme symbols into strings
(define (uglify-field-name field-name)
  (let ((str (symbol->string field-name)))
    ;; field? -> is-field
    (if (string-suffix? "?" str)
        (string-append "is-" (string-drop-right str 1))
      str)))

(define (serialize-string field-name value)
  #~(string-append #$(uglify-field-name field-name) " = " #$value "\n"))

(define (serialize-integer field-name value)
  (serialize-string field-name (number->string value)))

(define (serialize-boolean field-name value)
  (serialize-string field-name (if value "true" "false")))

(define (serialize-contact-name field-name value)
  #~(string-append "\n[" #$value "]\n"))

(define (list-of-contact-configurations? lst)
  (every contact-configuration? lst))

(define (serialize-list-of-contact-configurations field-name value)
  #~(string-append #$@(map (cut serialize-configuration <>
                                contact-configuration-fields)
                           value)))

(define (serialize-contacts-list-configuration configuration)
  (mixed-text-file
   "contactrc"
   #~(string-append "[Owner]\n"
                    #$(serialize-configuration
                       configuration contacts-list-configuration-fields))))

(define-maybe integer)
(define-maybe string)

(define-configuration contact-configuration
  (name
   (string)
   "The name of the contact."
   serialize-contact-name)
  (phone-number
   (maybe-integer 'disabled)
   "The person's phone number.")
  (email
   (maybe-string 'disabled)
   "The person's email address.")
  (married?
   (boolean)
   "Whether the person is married."))

(define-configuration contacts-list-configuration
  (name
   (string)
   "The name of the owner of this contact list.")
  (email
   (string)
   "The owner's email address.")
  (contacts
   (list-of-contact-configurations '())
   "A list of @code{contact-configuation} records which contain
     information about all your contacts."))

;; A contacts list configuration could then be created like this:

(define my-contacts
  (contacts-list-configuration
   (name "Alice")
   (email "alice@example.org")
   (contacts
    (list (contact-configuration
           (name "Bob")
           (phone-number 1234)
           (email "bob@gnu.org")
           (married? #f))
          (contact-configuration
           (name "Charlie")
           (phone-number 0000)
           (married? #t))))))
--8<---------------cut here---------------end--------------->8---


Evaluating it with will result:

--8<---------------cut here---------------start------------->8---
~ $ guile example.scm
;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
;;;       or pass the --no-auto-compile argument to disable.
;;; compiling /home/ss2/example.scm
;;; compiled /home/ss2/.cache/guile/ccache/3.0-LE-8-4.5/home/ss2/example.scm.go
Backtrace:
In ice-9/boot-9.scm:
  1752:10  9 (with-exception-handler _ _ #:unwind? _ # _)
In unknown file:
           8 (apply-smob/0 #<thunk 7f3e06d30080>)
In ice-9/boot-9.scm:
    724:2  7 (call-with-prompt _ _ #<procedure default-prompt-handle…>)
In ice-9/eval.scm:
    619:8  6 (_ #(#(#<directory (guile-user) 7f3e06d36c80>)))
In ice-9/boot-9.scm:
   2835:4  5 (save-module-excursion _)
  4380:12  4 (_)
In /home/ss2/example.scm:
     75:2  3 (_)
In srfi/srfi-1.scm:
    634:9  2 (for-each #<procedure 7f3e00139500 at gnu/services/con…> …)
In gnu/services/configuration.scm:
   121:24  1 (_ #<<configuration-field> name: contacts type: list-of…>)
In srfi/srfi-1.scm:
    241:2  0 (every _ _ . _)

srfi/srfi-1.scm:241:2: In procedure every:
In procedure every: Wrong type argument: #<syntax-transformer contact-configuration?>
--8<---------------cut here---------------end--------------->8---

Maybe something is wrong? 


Kind regards
Simon


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-28 13:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-24 16:13 Help on writing config serialiser Simon Streit
2022-01-28 13:13 ` Is the example about the serialiser in the manual correct? Was: " Simon Streit

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).