MAStringReader and nil

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

MAStringReader and nil

otto
Hi,

We'd like some help with how nil / undefined is handled in Magritte,
and specifically a MAStringDescription. The method MAStringReader >>
read:description: overrides the default behaviour to return nil. Is a
nil in Magritte seen as a "unpopulated" token?

Our problem more specifically is this:

We start with an object containing an string in an instance variable.
We use a MAStringDescription for this instance variable. We use the
magritte-seaside binding to edit this field on the UI. If the user
does not capture text in the text input, an empty string is "written".

The method on MAStringReader mentioned above intercepts this and
returns nil, so nil is written back to our domain object, where we
started off with an empty string.

We do not want nils to be written into our domain object where we
initialized it with an empty string.

How do we fix this? Is there a hook where we should be specifying
this? Or is there a fundamental way of working with Magritte-Seaside
where it will write nils and we have to manage that on our domain
object?

Thanks for your help.

Otto
_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|

Re: MAStringReader and nil

carlo.t
otto wrote
How do we fix this? Is there a hook where we should be specifying
this? Or is there a fundamental way of working with Magritte-Seaside
where it will write nils and we have to manage that on our domain
object?
Hi Otto

From what I could see it looks like a blank field will be interpreted as a nil, which means that the domain would need to check for nil in accessors and return a default.
Another option which I tried is to rather change MAMemento>>push: to make use of the #default: magritte description.
I.e. when creating your MAStringDescription send the #default: '' message then change MAMemento as per below so that if the memento cached value is nil, rather return the default value for the description (which by default is nil, MADescription class>>defaultDefault)

MAMemento>>push: aDictionary 
	"Utility method to push a dictionary mapping descriptions to values
	into the model."
	aDictionary
		keysAndValuesDo: [:key :value | self writeToModelUsingKey: key value: value]

writeToModelUsingKey: key value: value
	| modelValue |
	(key isVisible not or: [key isReadonly]) ifTrue: [^ self].
	modelValue := value isNil ifTrue: [key default yourself] ifFalse: [value].
	self model write: modelValue using: key.

Your mileage may vary but it looks pretty safe to use...
Reply | Threaded
Open this post in threaded view
|

Re: MAStringReader and nil

DiegoLont
In reply to this post by otto
Hi,

I myself prefer to handle these kind of things in the setters and getters, and not in the descriptions, as this protects the domain integrity also against other “wrong usages”, other then from Magritte. So I would recommend to have either

an accessor that returns an empty value if the value isn’t initialised:
someValue
        ^someValue ifNil: [ ‘’ ]

Or protect against writing a “nil” value:
someValue: anObject
        anObject
                ifNil: [ someValue := '' ]
                ifNotNil: [ someValue := anObject ]

But if you want to solve this on the magritte side, the best solution would be to set the reader to a reader that does not return nil if an empty string is written (stream atEnd).

Cheers,
Diego

On 06 Mar 2014, at 11:38, Otto Behrens <[hidden email]> wrote:

> Hi,
>
> We'd like some help with how nil / undefined is handled in Magritte,
> and specifically a MAStringDescription. The method MAStringReader >>
> read:description: overrides the default behaviour to return nil. Is a
> nil in Magritte seen as a "unpopulated" token?
>
> Our problem more specifically is this:
>
> We start with an object containing an string in an instance variable.
> We use a MAStringDescription for this instance variable. We use the
> magritte-seaside binding to edit this field on the UI. If the user
> does not capture text in the text input, an empty string is "written".
>
> The method on MAStringReader mentioned above intercepts this and
> returns nil, so nil is written back to our domain object, where we
> started off with an empty string.
>
> We do not want nils to be written into our domain object where we
> initialized it with an empty string.
>
> How do we fix this? Is there a hook where we should be specifying
> this? Or is there a fundamental way of working with Magritte-Seaside
> where it will write nils and we have to manage that on our domain
> object?
>
> Thanks for your help.
>
> Otto
> _______________________________________________
> Magritte, Pier and Related Tools ...
> https://www.iam.unibe.ch/mailman/listinfo/smallwiki


_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|

Re: MAStringReader and nil

otto
In reply to this post by carlo.t
> >From what I could see it looks like a blank field will be interpreted as a
> nil, which means that the domain would need to check for nil in accessors
> and return a default.
> Another option which I tried is to rather change MAMemento>>push: to make
> use of the #default: magritte description.
> I.e. when creating your MAStringDescription send the /#default: ''/ message
> then change MAMemento as per below so that if the memento cached value is
> nil, rather return the default value for the description (which by default
> is nil, MADescription class>>defaultDefault)

The way we handle instance variables thoughout our system is to
initialize with a default value (eg. empty string) and allow direct
instVar access. If we do the nil check in accessors (an acceptable
strategy I think), the we change this approach, and that is a bit of
work.

The default approach does not work on empty strings as it bypasses the
default code directly in the method that I mentioned here.
_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|

Re: MAStringReader and nil

otto
In reply to this post by DiegoLont
Thanks for the response. (And Carlo, thanks).

> someValue
>         ^someValue ifNil: [ '' ]

I can agree with this approach, but it will be quite a bit of work as
we generally initialize to empty strings / default values and allow
direct instVar access.

> Or protect against writing a "nil" value:
> someValue: anObject
>         anObject
>                 ifNil: [ someValue := '' ]
>                 ifNotNil: [ someValue := anObject ]

I like the changing getters more, this one feels a bit unexpected.
Anyhow, also quite a bit of work for us.

> But if you want to solve this on the magritte side, the best solution would be to set the reader to a reader that does not return nil if an empty string is written (stream atEnd).

Yes, sounds like the right way, thanks, will explore this.
_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki