git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 0/5] [RFC] config API: return empty list, not NULL

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Sep 28, 2022, 19:27 UTC
Message-ID
<220928.86czbfwb18.gmgdl@evledraar.gmail.com>
In-Reply-To
<35f6d380-f7a5-558d-1cfc-f5eb6ea6c818@github.com>
On Wed, Sep 28 2022, Derrick Stolee wrote:
Show 72 quoted lines
> On 9/27/2022 10:40 PM, Junio C Hamano wrote:
>> "Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com> writes:
>> 
>>> This work changes the behavior of asking for a multi-valued config key to
>>> return an empty list instead of a NULL value. This simplifies the handling
>>> of the result and is safer for development in the future.
>>>
>>> This is based on v4 of my unregister series [1]
>>>
>>> [1]
>>> https://lore.kernel.org/git/pull.1358.v4.git.1664287021.gitgitgadget@gmail.com/
>>>
>>> This idea came about due to a bug in the git maintenance unregister work
>>> where the result from git_config_get_value_multi() was sent directly to
>>> for_each_string_list_item() without checking for a NULL value first.
>>>
>>> I'm sending this as an RFC mostly because I'm not 100% sure this shift is
>>> worth the refactoring pain and effort. I personally think getting an empty
>>> list is a safer choice, but I can also understand if someone has a different
>>> opinion.
>> 
>> Thanks.
>> 
>> I actually am in favor of the idea that a NULL can be passed around
>> to signal the lack of a string_list (or the lack of a instance of
>> any "collection" type), and the current code is structured as such,
>> and it gives us extra flexibility.  Of course, we need to see if
>> that extra flexibility is worth it.
>> 
>> With a colleciton col, "if (col && col->nr)" checks if we have
>> something to work on.  But a code like this (which is a longhand for
>> the for_each_string_list_item() issue we just reencountered):
>> 
>>     col = git_get_some_collection(...);
>>     if (!col)
>> 	return; /* no collection */
>>     if (!col->nr)
>> 	git_add_to_some_collection(col, the default item);
>>     for (i = 0; i < col->nr; i++)
>> 	do things on col.stuff[i];
>> 
>> can react differently to cases where we have an empty collection
>> and where we do not have any collection to begin with.  
>> 
>> The other side of the coin is that it would make it harder to treat
>> the lack of collection itself and the collection being empty the
>> same way.  The above code might need to become
>> 
>>     col = git_get_some_collection(...);
>>     if (!col)
>> 	col = git_get_empty_collection();
>>     if (!col->nr)
>> 	git_add_to_some_collection(col, the default item);
>>     for (i = 0; i < col->nr; i++)
>> 	do things on col.stuff[i];
>> 
>> but if the "get the collection" thing returns an empty collection
>> when there is actually no collection, we can lose two lines from
>> there.
>
> I'm all for conveying more information when possible, but how can
> the config API provide a distinction between an empty list and a
> NULL list? The only thing I can think about is a case where the
> empty value clears the list and no new values are added, such as
>
> 	[bogus "key"]
> 		item = one
> 		item = two
> 		item =
>
> With this, the key exists in the config file, but the multi-valued
> list is empty.

It's not empty, that's a list with three items: ["one", "two", ""], the last one is just the empty string.

We then have some stateful parsing logic for individual keys that decides to apply the business logic that an empty value clears the list, but none of that's implemented as part of the config API itself.

I think we might want to create some helper function for such a special-cased "multi" list, but...

> Is that an important distinction? I don't think so.

...even then no, I don't think there should be a distinction. It shouldn't be an "empty list" in the "list.nr == 0" sense if the config API understood that sort of construct natively, it should just act as though the key wasn't specified, surely...

Previous: Derrick Stolee
Message 21 of 21 in “[RFC] config API: return empty list, not NULL”
  1. 0/5 [RFC] config API: return empty list, not NULLDerrick Stolee via GitGitGadget, Sep 27, 2022
  2. 1/5 config: relax requirements on multi-value returnDerrick Stolee via GitGitGadget, Sep 27, 2022
  3. Junio C HamanoSep 27, 2022
  4. 3/5 config: add BUG() statement instead of possible segfaultDerrick Stolee via GitGitGadget, Sep 27, 2022
  5. Ævar Arnfjörð BjarmasonSep 27, 2022
  6. Derrick StoleeSep 27, 2022
  7. Ævar Arnfjörð BjarmasonSep 27, 2022
  8. 4/5 config: return an empty list, not NULLDerrick Stolee via GitGitGadget, Sep 27, 2022
  9. Ævar Arnfjörð BjarmasonSep 27, 2022
  10. Derrick StoleeSep 27, 2022
  11. Ævar Arnfjörð BjarmasonSep 27, 2022
  12. Derrick StoleeSep 28, 2022
  13. Ævar Arnfjörð BjarmasonSep 28, 2022
  14. Derrick StoleeSep 28, 2022
  15. Ævar Arnfjörð BjarmasonSep 28, 2022
  16. 2/5 *: relax git_configset_get_value_multi resultDerrick Stolee via GitGitGadget, Sep 27, 2022
  17. Taylor BlauSep 28, 2022
  18. 5/5 *: expect a non-NULL list of config valuesDerrick Stolee via GitGitGadget, Sep 27, 2022
  19. Junio C HamanoSep 28, 2022
  20. Derrick StoleeSep 28, 2022
  21. Ævar Arnfjörð BjarmasonSep 28, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.