-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: make headerByGetter a Map (#171)
This commit turns the headerByGetter Object into a Map to reduces some code duplication an hopefully improve readability a little. Signed-off-by: Daniel Bevenius <daniel.bevenius@gmail.com>
- Loading branch information
Showing
4 changed files
with
66 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,35 @@ | ||
const { | ||
HEADER_CONTENT_TYPE, | ||
BINARY_HEADERS_03 | ||
BINARY_HEADERS_03 : { | ||
CONTENT_ENCODING, | ||
SUBJECT, | ||
TYPE, | ||
SPEC_VERSION, | ||
SOURCE, | ||
ID, | ||
TIME, | ||
SCHEMA_URL | ||
} | ||
} = require("../constants.js"); | ||
|
||
const passThroughParser = (v) => v; | ||
function parser(header, parser = (v) => v) { | ||
return { headerName: header, parse: parser }; | ||
} | ||
const passThroughParser = parser; | ||
|
||
/** | ||
* A utility object used to retrieve the header names for a CloudEvent | ||
* A utility Map used to retrieve the header names for a CloudEvent | ||
* using the CloudEvent getter function. | ||
*/ | ||
const headerByGetter = { | ||
getDataContentType: { | ||
name: HEADER_CONTENT_TYPE, | ||
parser: passThroughParser | ||
}, | ||
|
||
getDataContentEncoding: { | ||
name: BINARY_HEADERS_03.CONTENT_ENCODING, | ||
parser: passThroughParser | ||
}, | ||
|
||
getSubject: { | ||
name: BINARY_HEADERS_03.SUBJECT, | ||
parser: passThroughParser | ||
}, | ||
|
||
getType: { | ||
name: BINARY_HEADERS_03.TYPE, | ||
parser: passThroughParser | ||
}, | ||
|
||
getSpecversion: { | ||
name: BINARY_HEADERS_03.SPEC_VERSION, | ||
parser: passThroughParser | ||
}, | ||
|
||
getSource: { | ||
name: BINARY_HEADERS_03.SOURCE, | ||
parser: passThroughParser | ||
}, | ||
|
||
getId: { | ||
name: BINARY_HEADERS_03.ID, | ||
parser: passThroughParser | ||
}, | ||
|
||
getTime: { | ||
name: BINARY_HEADERS_03.TIME, | ||
parser: passThroughParser | ||
}, | ||
|
||
getSchemaurl: { | ||
name: BINARY_HEADERS_03.SCHEMA_URL, | ||
parser: passThroughParser | ||
} | ||
}; | ||
|
||
module.exports = headerByGetter; | ||
const headerMap = new Map(); | ||
headerMap.set('getDataContentType', passThroughParser(HEADER_CONTENT_TYPE)); | ||
headerMap.set('getDataContentEncoding', passThroughParser(CONTENT_ENCODING)); | ||
headerMap.set('getSubject', passThroughParser(SUBJECT)); | ||
headerMap.set('getType', passThroughParser(TYPE)); | ||
headerMap.set('getSpecversion', passThroughParser(SPEC_VERSION)); | ||
headerMap.set('getSource', passThroughParser(SOURCE)); | ||
headerMap.set('getId', passThroughParser(ID)); | ||
headerMap.set('getTime', passThroughParser(TIME)); | ||
headerMap.set('getSchemaurl', passThroughParser(SCHEMA_URL)); | ||
|
||
module.exports = headerMap; |
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 |
---|---|---|
@@ -1,54 +1,33 @@ | ||
const { | ||
HEADER_CONTENT_TYPE, | ||
BINARY_HEADERS_1 | ||
BINARY_HEADERS_1 : { | ||
SUBJECT, | ||
TYPE, | ||
SPEC_VERSION, | ||
SOURCE, | ||
ID, | ||
TIME, | ||
DATA_SCHEMA | ||
} | ||
} = require("../constants.js"); | ||
|
||
const passThroughParser = (v) => v; | ||
function parser(header, parser = v => v) { | ||
return { headerName: header, parse: parser }; | ||
} | ||
const passThroughParser = parser; | ||
|
||
/** | ||
* A utility object used to retrieve the header names for a CloudEvent | ||
* A utility Map used to retrieve the header names for a CloudEvent | ||
* using the CloudEvent getter function. | ||
*/ | ||
const headerByGetter = { | ||
getDataContentType: { | ||
name: HEADER_CONTENT_TYPE, | ||
parser: passThroughParser | ||
}, | ||
|
||
getSubject: { | ||
name: BINARY_HEADERS_1.SUBJECT, | ||
parser: passThroughParser | ||
}, | ||
|
||
getType: { | ||
name: BINARY_HEADERS_1.TYPE, | ||
parser: passThroughParser | ||
}, | ||
|
||
getSpecversion: { | ||
name: BINARY_HEADERS_1.SPEC_VERSION, | ||
parser: passThroughParser | ||
}, | ||
|
||
getSource: { | ||
name: BINARY_HEADERS_1.SOURCE, | ||
parser: passThroughParser | ||
}, | ||
|
||
getId: { | ||
name: BINARY_HEADERS_1.ID, | ||
parser: passThroughParser | ||
}, | ||
|
||
getTime: { | ||
name: BINARY_HEADERS_1.TIME, | ||
parser: passThroughParser | ||
}, | ||
|
||
getDataschema: { | ||
name: BINARY_HEADERS_1.DATA_SCHEMA, | ||
parser: passThroughParser | ||
} | ||
}; | ||
|
||
module.exports = headerByGetter; | ||
const headerMap = new Map(); | ||
headerMap.set('getDataContentType', passThroughParser(HEADER_CONTENT_TYPE)); | ||
headerMap.set('getSubject', passThroughParser(SUBJECT)); | ||
headerMap.set('getType', passThroughParser(TYPE)); | ||
headerMap.set('getSpecversion', passThroughParser(SPEC_VERSION)); | ||
headerMap.set('getSource', passThroughParser(SOURCE)); | ||
headerMap.set('getId', passThroughParser(ID)); | ||
headerMap.set('getTime', passThroughParser(TIME)); | ||
headerMap.set('getDataschema', passThroughParser(DATA_SCHEMA)); | ||
|
||
module.exports = headerMap; |