Skip to content

Commit

Permalink
[Enh]: Magritte Writer - Sub. Desc. & Ignore Unknown Fields
Browse files Browse the repository at this point in the history
- Specify a substitute description
- Option to skip unknown fields instead of signalling an error
  • Loading branch information
seandenigris committed Nov 19, 2023
1 parent e059c21 commit e5c4799
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
28 changes: 24 additions & 4 deletions repository/Neo-CSV-Magritte/MACSVWriter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Class {
'target',
'subjects',
'map',
'subjectDescription'
'subjectDescription',
'ignoresUnkownFields'
],
#category : #'Neo-CSV-Magritte-Visitors'
}
Expand Down Expand Up @@ -45,6 +46,16 @@ MACSVWriter >> header [
collect: [ :field | field propertyAt: self fieldNamePropertyKey ifAbsent: [ field name ] ]
]

{ #category : #accessing }
MACSVWriter >> ignoresUnknownFields [
^ ignoresUnkownFields ifNil: [ false ]
]

{ #category : #accessing }
MACSVWriter >> ignoresUnknownFields: anObject [
ignoresUnkownFields := anObject
]

{ #category : #accessing }
MACSVWriter >> includesHeader [
^ includesHeader ifNil: [ true ].
Expand Down Expand Up @@ -74,11 +85,16 @@ MACSVWriter >> map: aString fieldDo: aBlock [
self map add: field.
]

{ #category : #private }
{ #category : #accessing }
MACSVWriter >> subjectDescription [
^ subjectDescription ifNil: [ subjectDescription := self subjects first magritteDescription ]
]

{ #category : #accessing }
MACSVWriter >> subjectDescription: anMAContainer [
subjectDescription := anMAContainer
]

{ #category : #accessing }
MACSVWriter >> subjects [
^ subjects
Expand Down Expand Up @@ -109,11 +125,15 @@ MACSVWriter >> writeToStream: aStream [

self fieldDescriptions
do: [ :field |
| converter |
| converter wrappedConverter |
converter := field
propertyAt: self fieldWriterPropertyKey
ifAbsent: [ [ :anObject | field read: anObject ] ].
self writer addField: converter ].
wrappedConverter := [ :anObject |
(self ignoresUnknownFields not or: [ field accessor canRead: anObject ])
ifTrue: [ converter value: anObject ]
ifFalse: [ nil ] ].
self writer addField: wrappedConverter ].

writer := self writer on: aStream.

Expand Down
52 changes: 52 additions & 0 deletions repository/Neo-CSV-Magritte/MACSVWriterTests.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,55 @@ MACSVWriterTests >> testDefaultMapping [
"Alan Kay","","17 May 1940"
' withUnixLineEndings.
]

{ #category : #tests }
MACSVWriterTests >> testIgnoreUnknownFields [

| field substitution unknownField |
field := MACSVTestPerson magritteTemplate birthdateDescription.
unknownField := MAStringDescription new
accessor: #unknownSelector;
csvFieldName: 'Unknown';
priority: 10;
yourself.
substitution := MAContainer withAll: { field. unknownField }.

writer
subjectDescription: substitution;
ignoresUnknownFields: true;
execute.

self assert: target contents equals: '"DOB","Unknown"
"17 May 1940",""
' withUnixLineEndings.
]

{ #category : #tests }
MACSVWriterTests >> testSubjectDescriptionSubstitution [

writer
subjectDescription: MACSVTestPerson magritteTemplate birthdateDescription asContainer;
execute.

self assert: target contents equals: '"DOB"
"17 May 1940"
' withUnixLineEndings.
]

{ #category : #tests }
MACSVWriterTests >> testUnknownFields [

| unknownField |
unknownField := MAStringDescription new
accessor: #unknownSelector;
csvFieldName: 'Unknown';
priority: 10;
yourself.

self
should: [
writer
subjectDescription: unknownField asContainer;
execute ]
raise: MessageNotUnderstood.
]

0 comments on commit e5c4799

Please sign in to comment.