[Zope3-dev] Should containers tell you if they accept empty string keys?

Jim Fulton jim@zope.com
Mon, 18 Nov 2002 13:30:17 -0500


Steve Alexander wrote:
> When you call setObject on a container, you must provide string to use 
> as a key. Depending on the implementation, a container can:
> 
> * Use the key you provided
> 
> * Use some modified form of the key you provided
> 
> * Ignore the key you provided, and generate its own key
> 
> The return value of setObject is the key the container used.
> 
> A common protocol is to
> 
> * Use the key provided, unless it is an empty string, in which case
>   generate a key
> 
> An empty string is not a valid value for the key of a container. So, if 
> a container accepts an empty string as a key, it must provide its own key.
> 
> 
> Short of actually trying to add an object using an empty string key, 
> there is in general no way of knowing if a container accepts 
> empty-string keys.
> 
> This causes a problem with adding views that have several pages 
> presented in sequence: the "no name provided" error only comes up after 
> the last page, when the user has already done some work, and when it is 
> too late to correct the problem.
> 
> Solution:
> 
> Either of the following approaches would solve the problem.
> 
> * The first page of a multi-page adding view needs to check that 
> contentName is set on the Adder.
> 
>   - This introduces a coupling between adder views and the
>     implementation of the container that is being added to.
> 
> 
> * There is some way of checking whether the container supports empty 
> string keys before actually adding an object, so that the UI can be 
> presented appropriately.
> 
>   - This makes the IWriteContainer interface slightly more complex.
> 
> 
> I favour the latter approach, as I really don't like the coupling 
> implied by the former.

A third approach is to define three new interfaces that extend
IWriteContainer (or IContainer) that indicate whether the container:

- Never generates a key

- Always generates a key

- Generates a key of one osn't given.

There could be specific adders for these or a general adder could do
introspection and complain if an id is required but not provided.

Jim

-- 
Jim Fulton           mailto:jim@zope.com       Python Powered!
CTO                  (888) 344-4332            http://www.python.org
Zope Corporation     http://www.zope.com       http://www.zope.org