[Pharo-users] NeoJSON and inheritance

Sven Van Caekenberghe sven at stfx.eu
Sat Mar 22 06:15:35 EDT 2014


Phil,

I finally found time to look in to this in detail and added a test with inheritance.

In #bleedingEdge:

===
Name: Neo-JSON-Core-SvenVanCaekenberghe.25
Author: SvenVanCaekenberghe
Time: 22 March 2014, 11:03:31.510538 am
UUID: 3051cd7d-a857-4aee-b5ba-d56a4085517b
Ancestors: Neo-JSON-Core-SvenVanCaekenberghe.24

Added basic support for mapping using inheritance when using #neoJonMapping : (see #testObject3)
Renamed #new[Custom|Object]MappingFor: to [custom|object]MappingFor: to better reflect the new behavior
Thanks  Philippe Back for making these suggestions!
===
Name: Neo-JSON-Tests-SvenVanCaekenberghe.23
Author: SvenVanCaekenberghe
Time: 22 March 2014, 11:04:11.513921 am
UUID: 27fad240-c6b9-4d00-a4e6-6e49fd612dfb
Ancestors: Neo-JSON-Tests-SvenVanCaekenberghe.22

Added basic support for mapping using inheritance when using #neoJonMapping : (see #testObject3)
Renamed #new[Custom|Object]MappingFor: to [custom|object]MappingFor: to better reflect the new behavior
Thanks  Philippe Back for making these suggestions!
===

On 17 Mar 2014, at 18:01, phil at highoctane.be wrote:

> NeoJSONMapper>>newObjectMappingFor: smalltalkClass
> 	| mapping |
> 
> 	mapping := self mappings at: smalltalkClass ifAbsent: [ 
> 		mapping := NeoJSONObjectMapping new.
> 		mapping subjectClass: smalltalkClass.
> 		self mappings at: smalltalkClass put: mapping.
> 	].
> 
> 	^ mapping
> 
> 
> works better ...

#at:ifAbsentPut: is even better ;-)

Regards,

Sven

> Phil
> 
> On Mon, Mar 17, 2014 at 5:40 PM, phil at highoctane.be <phil at highoctane.be> wrote:
> The "super" thing doesn't work.
> 
> I have spent a while tracing what was occuring with the custom mappings.
> 
> If in a subclass I do:
> 
> neoJsonMapping: aMapper
> 	super neoJsonMapping: aMapper.
> 	
> 	aMapper for: self do: [ :mapping |
> 		mapping	
> 			mapProperty: 'startDate' getter: [ :filter | filter startDate ]	setter: [ :filter :value | filter startDate: value ];
> 			mapProperty: 'endDate'	getter: [ :filter | filter endDate ]	setter: [ :filter :value | filter endDate: value ].
> 	]
> 
> and in the superclass:
> 
> neoJsonMapping: aMapper
> 	aMapper for: self do: [ :mapping |
> 		mapping mapInstVar: #id to: 'id'.
> 	]
> 
> (do not pay attention to mapInstVar or mapProperty, I was figuring out how things worked).
> 
> Thing is that the mapping goes fine up to one point.
> 
> super neoJsonMapping: aMapper 
> 
> indeed puts the right 'id' mapping but then 
> 
> aMapper for:: self do: [ ...
> 
> does
> 
> for: smalltalkClass do: block
> 	"Create and add a new standard object mapping for smalltalkClass.
> 	The code in block should further customize the mapping."
> 	
> 	| mapping |
> 	mapping := self newObjectMappingFor: smalltalkClass.
> 	block value: mapping.
> 	^ mapping
> 
> 
> which in turn:
> 
> newObjectMappingFor: smalltalkClass
> 	| mapping |
> 	mapping := NeoJSONObjectMapping new.
> 	mapping subjectClass: smalltalkClass.
> 	self mappings at: smalltalkClass put: mapping.
> 	^ mapping
> 
> 
> 
> Argh: 	self mappings at: smalltalkClass put: mapping.
> 
> Overwrites the existing contents.
> 
> So, only the entries of the subclass are left.
> 
> I guess that's a bug. 
> 
> I'd do:
> 
> newObjectMappingFor: smalltalkClass
> 	| mapping |
> 
> 	mapping := self mappings at: smalltalkClass.
> 
> 	mapping ifNil: [ 
> 		mapping := NeoJSONObjectMapping new.
> 		mapping subjectClass: smalltalkClass.
> 		self mappings at: smalltalkClass put: mapping.
> 	].
> 
> 	^ mapping
> 
> instead.
> 
> Does this look right? 
> 
> mapping := self newObjectMappingFor: smalltalkClass.
> 
> isn't then really intention revealing. Shouldn't we rename that to objectMappingFor: smalltalkClass
> 
> 
> Phil
> 
> 
> 
> 
> 
> 
> On Fri, Sep 27, 2013 at 7:07 PM, Sven Van Caekenberghe <sven at stfx.eu> wrote:
> Norbert,
> 
> On 26 Sep 2013, at 11:32, Norbert Hartl <norbert at hartl.name> wrote:
> 
> > How is inheritance supposed to work in NeoJSON? I only figured it out by doing that manually:
> >
> > neoJsonMapping: aMapper
> >       super neoJsonMapping: aMapper.
> >       (aMapper mappingFor: self)
> >                       mapInstVar: #longitude;
> >                       mapInstVar: #latitude.
> >
> > Is there a better way to do?
> >
> > Norbert
> 
> What you figured out yourself is the only inheritance that is available.
> 
> The thing is, schema names (which can be plain Symbols or Class objects) are meant to be used for things like #ArrayOfPoints, #DictionaryWithUserValues which tell something about subtypes. I hurts to think of an inheritance for that ;-)
> 
> Sven
> 
> 
> 





More information about the Pharo-users mailing list