guix-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Best practices for writing services


From: Maxim Cournoyer
Subject: Re: Best practices for writing services
Date: Fri, 23 Apr 2021 00:42:41 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)

Hello,

Xinglu Chen <public@yoctocell.xyz> writes:

> On Wed, Apr 21 2021, Maxim Cournoyer wrote:
>
>>> One thing that I find a little annoying is that you have to specify a
>>> default value for the fields.  This doesn’t make much sense in some
>>> cases, e.g. there is no good default value for ‘user.name = NAME’ in the
>>> Git config, the user should have to specify that themselves.
>>
>> There's a 'define-maybe' that can be used to declare a field that can
>> take more than one type, e.g.:
>>
>> (define-maybe string)
>>
>> Will generate a definition like so:
>>
>> --8<---------------cut here---------------start------------->8---
>> (define (maybe-string? val)
>>                (or (eq? val 'disabled) (string? val)))
>> --8<---------------cut here---------------end--------------->8---
>>
>> Which the validator of define-configuration will use if you specify a
>> field with the type 'maybe-string'.
>>
>> 'disabled is a bit semantically broken in some cases ('unspecified'
>> could be nicer), but it does the job.
>
> But the problem here is that it doesn’t force the user to configure the
> field.  In a Git config for example, the user should be forced to set
> ‘user.name’ and ‘user.email’, otherwise they can’t commit anything.  You
> will just have to set the default value to ‘disabled’, like this:
>
> #+begin_src scheme
> (define (serialize-string field-name val) ...)
> (define-maybe string)
> (define-configuration test-config
>   (config
>     (maybe-string ’disabled))
>     "docs"")
> #+end_src

Ah, thanks for explaining, now I understand your point well.

I've just tried something:

--8<---------------cut here---------------start------------->8---
(define-configuration test-config
                       (name (string #f) "Your name"))
scheme@(guile-user)> (test-config)
ice-9/boot-9.scm:1669:16: In procedure raise-exception:
ERROR:
  1. &message: "Invalid value for field name: #f"
  2. &configuration-error
--8<---------------cut here---------------end--------------->8---

So you could choose an invalid default value, which would force the user
to specify it (else they'd get the not so obvious error message above).
It should be improved too!  I'll see if I can do something.

Thanks!

Maxim



reply via email to

[Prev in Thread] Current Thread [Next in Thread]