-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow using instance creation methods when reading NeoJSON objects
- Loading branch information
Showing
4 changed files
with
208 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
source/Stargate-NeoJSON-Extensions/ValidCompleteInstanceMapping.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
" | ||
I am NeoJSONStrictObjectMapping, I'm equivalent to NeoJSONObjectMapping but more strict. | ||
I will fail on reading properties of an object if some of the mapped properties are missing in the incoming JSON. | ||
" | ||
Class { | ||
#name : 'ValidCompleteInstanceMapping', | ||
#superclass : 'NeoJSONObjectMapping', | ||
#instVars : [ | ||
'instanceCreationSelector', | ||
'argumentNames' | ||
], | ||
#category : 'Stargate-NeoJSON-Extensions', | ||
#package : 'Stargate-NeoJSON-Extensions' | ||
} | ||
|
||
{ #category : 'private' } | ||
ValidCompleteInstanceMapping >> errorDescriptionForMissing: propertyNames [ | ||
|
||
^ String streamContents: [ :stream | | ||
stream | ||
nextPutAll: 'Missing required keys'; | ||
space; | ||
nextPut: $(. | ||
propertyNames | ||
do: [ :propertyName | | ||
stream | ||
nextPut: $#; | ||
nextPutAll: propertyName | ||
] | ||
separatedBy: [ | ||
stream | ||
nextPut: $,; | ||
space | ||
]. | ||
stream nextPut: $) | ||
] | ||
] | ||
|
||
{ #category : 'mapping' } | ||
ValidCompleteInstanceMapping >> mapCreationSending: anInstanceCreationSelector withArguments: anArgumentCollection [ | ||
|
||
instanceCreationSelector := anInstanceCreationSelector. | ||
argumentNames := anArgumentCollection | ||
] | ||
|
||
{ #category : 'mapping' } | ||
ValidCompleteInstanceMapping >> mapProperty: aKey [ | ||
|
||
^ self | ||
mapProperty: aKey | ||
getter: [ :object | ] | ||
setter: [ :arguments :value | arguments at: aKey put: value ] | ||
] | ||
|
||
{ #category : 'mapping' } | ||
ValidCompleteInstanceMapping >> mapProperty: aKey as: aValueSchema [ | ||
|
||
( self mapProperty: aKey ) valueSchema: aValueSchema | ||
] | ||
|
||
{ #category : 'parsing' } | ||
ValidCompleteInstanceMapping >> readFrom: jsonReader [ | ||
|
||
| argumentByName arguments missingArguments | | ||
|
||
argumentByName := Dictionary new. | ||
jsonReader parseMapKeysDo: [ :key | | ||
( self propertyNamed: key ifAbsent: [ nil ] ) | ||
ifNil: [ "read, skip & ignore value" jsonReader next ] | ||
ifNotNil: [ :mapping | mapping readObject: argumentByName from: jsonReader ] | ||
]. | ||
|
||
missingArguments := OrderedCollection new. | ||
arguments := argumentNames collect: [ :argumentName | | ||
argumentByName at: argumentName ifAbsent: [ missingArguments add: argumentName ] ]. | ||
missingArguments ifNotEmpty: [ | ||
jsonReader error: ( self errorDescriptionForMissing: missingArguments ) ]. | ||
|
||
^ subjectClass perform: instanceCreationSelector withArguments: arguments | ||
] |