Skip to content

Commit

Permalink
Merge pull request #43 from seandenigris/enh_ma-manual-column-names
Browse files Browse the repository at this point in the history
[Enh]: Magritte Importer - Allow Manual Setting of Column Names
  • Loading branch information
seandenigris committed Oct 20, 2023
2 parents 63cbd24 + 30a5df9 commit 0ba1afb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
49 changes: 35 additions & 14 deletions repository/Neo-CSV-Magritte/MACSVImporter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,33 @@ Class {
'reader',
'readerClass',
'recordClass',
'source'
'source',
'columnNames'
],
#category : #'Neo-CSV-Magritte-Visitors'
}

{ #category : #accessing }
MACSVImporter >> columnNames [
"See setter comment"

^ columnNames
]

{ #category : #accessing }
MACSVImporter >> columnNames: aCollection [
"If not supplied, will assume the first row contains them"

columnNames := aCollection
]

{ #category : #private }
MACSVImporter >> configureReaderFor: aStream [
self reader on: aStream.
self columnNames ifNil: [ self columnNames: self readHeader ].
self reader recordClass: self recordClass.
]

{ #category : #accessing }
MACSVImporter >> execute [
^ self source isStream
Expand All @@ -33,23 +55,22 @@ MACSVImporter >> fieldReaderPropertyKey [

{ #category : #private }
MACSVImporter >> importStream: aStream [
| fields |
self configureReaderFor: aStream.

| fields header |
self reader on: aStream.
header := self readHeader.
self reader recordClass: self recordClass.
fields := self recordClass new magritteDescription children.
header do: [ :h |
fields
detect: [ :f |
f
propertyAt: self fieldNamePropertyKey
self columnNames
do: [ :h |
fields
detect: [ :f |
f
propertyAt: self fieldNamePropertyKey
ifPresent: [ :fieldName | fieldName = h asString trimmed ]
ifAbsent: [ false ] ]
ifFound: [ :e |
self flag: 'need a way to customize the reader here'.
self reader addFieldDescribedByMagritte: e ]
ifNone: [ self reader addIgnoredField ] ].
ifFound: [ :e |
self flag: 'need a way to customize the reader here'.
self reader addFieldDescribedByMagritte: e ]
ifNone: [ self reader addIgnoredField ] ].
^ self reader upToEnd "or do more processing e.g. `select: [ :record | record lastName isNotNil ]`"
]

Expand Down
29 changes: 19 additions & 10 deletions repository/Neo-CSV-Magritte/MACSVTwoStageImporter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ Class {
#category : #'Neo-CSV-Magritte-Visitors'
}

{ #category : #accessing }
MACSVTwoStageImporter >> configureReaderFor: aStream [

super configureReaderFor: aStream.

self reader
emptyFieldValue: #passNil;
addFieldsAt: (self columnNames collect: [ :each | each asSymbol ]) "Adapted from NeoCSVReader>>#namedColumnsConfiguration"
]

{ #category : #accessing }
MACSVTwoStageImporter >> convertToDomainObjects: aCollectionOfDictionaries [
^ aCollectionOfDictionaries
Expand All @@ -33,11 +43,18 @@ MACSVTwoStageImporter >> domainObjectFromDictionary: aDictionary [
{ #category : #accessing }
MACSVTwoStageImporter >> importStream: aStream [
| rows |
self reader on: aStream.
rows := self readInput.
self configureReaderFor: aStream.
rows := self reader upToEnd.
^ self convertToDomainObjects: rows
]

{ #category : #accessing }
MACSVTwoStageImporter >> initialize [

super initialize.
self recordClass: Dictionary.
]

{ #category : #accessing }
MACSVTwoStageImporter >> initializeDomainObject: anObject fromRecord: aDictionary [
"We needed an instance-side version because some objects may need configuration during instance creation"
Expand Down Expand Up @@ -101,14 +118,6 @@ MACSVTwoStageImporter >> preprocessor: anObject [
preprocessor := anObject
]

{ #category : #accessing }
MACSVTwoStageImporter >> readInput [
^ self reader
emptyFieldValue: #passNil;
namedColumnsConfiguration;
upToEnd.
]

{ #category : #accessing }
MACSVTwoStageImporter >> selectBlock [
^ selectBlock ifNil: [ #isNotNil ]
Expand Down

0 comments on commit 0ba1afb

Please sign in to comment.