unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Maxim Cournoyer <maxim.cournoyer@gmail.com>
To: Xinglu Chen <public@yoctocell.xyz>
Cc: guix-devel@gnu.org
Subject: Re: Best practices for writing services
Date: Fri, 23 Apr 2021 00:42:41 -0400	[thread overview]
Message-ID: <87tunx1uim.fsf@gmail.com> (raw)
In-Reply-To: <87y2da6aon.fsf@yoctocell.xyz> (Xinglu Chen's message of "Thu, 22 Apr 2021 09:27:36 +0200")

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	other threads:[~2021-04-23  4:43 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-18 15:23 Best practices for writing services Xinglu Chen
2021-03-19 16:12 ` Joshua Branson
2021-03-19 17:01   ` Xinglu Chen
2021-04-21  3:54 ` Maxim Cournoyer
2021-04-21 10:45   ` Xinglu Chen
2021-04-21 18:05     ` Maxim Cournoyer
2021-04-22  7:27       ` Xinglu Chen
2021-04-23  4:42         ` Maxim Cournoyer [this message]
2021-04-23  6:04           ` Xinglu Chen
2021-04-21 22:10     ` raingloom
2021-04-22  7:19       ` Xinglu Chen
2021-04-26 20:29         ` raingloom

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87tunx1uim.fsf@gmail.com \
    --to=maxim.cournoyer@gmail.com \
    --cc=guix-devel@gnu.org \
    --cc=public@yoctocell.xyz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

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).