New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode based on column names #6

Closed
gwils opened this Issue Mar 6, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@gwils
Collaborator

gwils commented Mar 6, 2018

Add the ability to decode a field based on its name as found in the header, to complement the current position-based decoding.

@kanishka-azimi

This comment has been minimized.

kanishka-azimi commented Mar 20, 2018

Example usage https://github.com/databrary/databrary/blob/kanishka-dbd-845-b2/src/Databrary/Model/Ingest.hs#L373 , pasted inline:

participantRecordParseNamedRecord :: ParticipantFieldMapping -> Csv.NamedRecord -> Parser ParticipantRecord
participantRecordParseNamedRecord fieldMap m = do
    mId <- extractIfUsed2 pfmId validateParticipantId
    mInfo <- extractIfUsed2 pfmInfo validateParticipantInfo
    mDescription <- extractIfUsed2 pfmDescription validateParticipantDescription
    mBirthdate <- extractIfUsed2 pfmBirthdate validateParticipantBirthdate
    mGender <- extractIfUsed2 pfmGender validateParticipantGender
    mRace <- extractIfUsed2 pfmRace validateParticipantRace
    mEthnicity <- extractIfUsed2 pfmEthnicity validateParticipantEthnicity
    mGestationalAge <- extractIfUsed2 pfmGestationalAge validateParticipantGestationalAge
    mPregnancyTerm <- extractIfUsed2 pfmPregnancyTerm validateParticipantPregnancyTerm
    mBirthWeight <- extractIfUsed2 pfmBirthWeight validateParticipantBirthWeight
    mDisability <- extractIfUsed2 pfmDisability validateParticipantDisability
    mLanguage <- extractIfUsed2 pfmLanguage validateParticipantLanguage
    mCountry <- extractIfUsed2 pfmCountry validateParticipantCountry
    mState <- extractIfUsed2 pfmState validateParticipantState
    mSetting <- extractIfUsed2 pfmSetting validateParticipantSetting
    pure
        (ParticipantRecord
            { prdId = mId
            , prdInfo = mInfo
            , prdDescription = mDescription
            , prdBirthdate = mBirthdate
            , prdGender = mGender
            , prdRace = mRace
            , prdEthnicity = mEthnicity
            , prdGestationalAge = mGestationalAge
            , prdPregnancyTerm = mPregnancyTerm
            , prdBirthWeight = mBirthWeight
            , prdDisability = mDisability
            , prdLanguage = mLanguage
            , prdCountry = mCountry
            , prdState = mState
            , prdSetting = mSetting
            } )
  where
    extractIfUsed2
      :: (ParticipantFieldMapping -> Maybe Text) -> (BS.ByteString -> Maybe BS.ByteString) -> Parser (Maybe BS.ByteString)
    extractIfUsed2 maybeGetField validateValue = do
        case maybeGetField fieldMap of
            Just colName -> do
                contents <- m .: (TE.encodeUtf8 colName)
                maybe
                    (fail ("invalid value for " ++ show colName ++ ", found " ++ show contents))
                    (pure . Just)
                    (validateValue contents)
            Nothing ->
                pure Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment