Removed unneccessary mapMayHaveKeyObjects

This commit is contained in:
Felix Krause 2016-01-11 19:12:55 +01:00
parent f5627254f7
commit 1e39ca5b6c
3 changed files with 69 additions and 96 deletions

View File

@ -54,10 +54,8 @@ proc endDocEvent*(): YamlStreamEvent =
result = YamlStreamEvent(kind: yamlEndDocument) result = YamlStreamEvent(kind: yamlEndDocument)
proc startMapEvent*(tag: TagId = yTagQuestionMark, proc startMapEvent*(tag: TagId = yTagQuestionMark,
anchor: AnchorId = yAnchorNone, anchor: AnchorId = yAnchorNone): YamlStreamEvent =
mayHaveKeyObjects: bool = true): YamlStreamEvent = result = YamlStreamEvent(kind: yamlStartMap, mapTag: tag, mapAnchor: anchor)
result = YamlStreamEvent(kind: yamlStartMap, mapTag: tag, mapAnchor: anchor,
mapMayHaveKeyObjects: mayHaveKeyObjects)
proc endMapEvent*(): YamlStreamEvent = proc endMapEvent*(): YamlStreamEvent =
result = YamlStreamEvent(kind: yamlEndMap) result = YamlStreamEvent(kind: yamlEndMap)

View File

@ -6,11 +6,10 @@
type type
DumperState = enum DumperState = enum
dBlockExplicitMapKey, dBlockExplicitMapValue, dBlockImplicitMapKey, dBlockExplicitMapKey, dBlockImplicitMapKey, dBlockMapValue,
dBlockImplicitMapValue, dBlockSequenceItem, dFlowImplicitMapKey, dBlockSequenceItem, dFlowImplicitMapKey, dFlowMapValue,
dFlowImplicitMapValue, dFlowExplicitMapKey, dFlowExplicitMapValue, dFlowExplicitMapKey, dFlowSequenceItem,
dFlowSequenceItem, dFlowImplicitMapStart, dFlowExplicitMapStart, dFlowMapStart, dFlowSequenceStart
dFlowSequenceStart
proc needsEscaping(scalar: string): bool {.raises: [].} = proc needsEscaping(scalar: string): bool {.raises: [].} =
scalar.len == 0 or scalar.len == 0 or
@ -40,56 +39,51 @@ template safeWrite(s: string or char) {.dirty.} =
raise e raise e
proc startItem(target: Stream, style: YamlPresentationStyle, indentation: int, proc startItem(target: Stream, style: YamlPresentationStyle, indentation: int,
state: var DumperState) {.raises: [YamlPresenterOutputError].} = state: var DumperState, isObject: bool)
{.raises: [YamlPresenterOutputError].} =
try: try:
case state case state
of dBlockExplicitMapValue: of dBlockMapValue:
target.write('\x0A') target.write('\x0A')
target.write(repeat(' ', indentation)) target.write(repeat(' ', indentation))
target.write("? ") if isObject or style == ypsCanonical:
state = dBlockExplicitMapKey target.write("? ")
state = dBlockExplicitMapKey
else:
state = dBlockImplicitMapKey
of dBlockExplicitMapKey: of dBlockExplicitMapKey:
target.write('\x0A') target.write('\x0A')
target.write(repeat(' ', indentation)) target.write(repeat(' ', indentation))
target.write(": ") target.write(": ")
state = dBlockExplicitMapValue state = dBlockMapValue
of dBlockImplicitMapValue:
target.write('\x0A')
target.write(repeat(' ', indentation))
state = dBlockImplicitMapKey
of dBlockImplicitMapKey: of dBlockImplicitMapKey:
target.write(": ") target.write(": ")
state = dBlockImplicitMapValue state = dBlockMapValue
of dFlowExplicitMapKey: of dFlowExplicitMapKey:
target.write('\x0A') target.write('\x0A')
target.write(repeat(' ', indentation)) target.write(repeat(' ', indentation))
target.write(": ") target.write(": ")
state = dFlowExplicitMapValue state = dFlowMapValue
of dFlowExplicitMapValue: of dFlowMapValue:
target.write(",\x0A") if isObject or style in [ypsJson, ypsCanonical]:
target.write(repeat(' ', indentation)) target.write(",\x0A" & repeat(' ', indentation))
target.write("? ") if style != ypsJson:
state = dFlowExplicitMapKey target.write("? ")
of dFlowImplicitMapStart: state = dFlowExplicitMapKey
if style == ypsJson:
target.write("\x0A")
target.write(repeat(' ', indentation))
state = dFlowImplicitMapKey
of dFlowExplicitMapStart:
target.write('\x0A')
target.write(repeat(' ', indentation))
target.write("? ")
state = dFlowExplicitMapKey
of dFlowImplicitMapKey:
target.write(": ")
state = dFlowImplicitMapValue
of dFlowImplicitMapValue:
if style == ypsJson:
target.write(",\x0A")
target.write(repeat(' ', indentation))
else: else:
target.write(", ") target.write(", ")
state = dFlowImplicitMapKey state = dFlowImplicitMapKey
of dFlowMapStart:
if isObject or style in [ypsJson, ypsCanonical]:
target.write("\x0A" & repeat(' ', indentation))
if style != ypsJson:
target.write("? ")
state = dFlowExplicitMapKey
else:
state = dFlowImplicitMapKey
of dFlowImplicitMapKey:
target.write(": ")
state = dFlowMapValue
of dBlockSequenceItem: of dBlockSequenceItem:
target.write('\x0A') target.write('\x0A')
target.write(repeat(' ', indentation)) target.write(repeat(' ', indentation))
@ -185,7 +179,8 @@ proc present*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
if style != ypsJson: if style != ypsJson:
safeWrite('\x0A') safeWrite('\x0A')
else: else:
startItem(target, style, indentation, levels[levels.high]) startItem(target, style, indentation, levels[levels.high],
false)
if style != ypsJson: if style != ypsJson:
writeTagAndAnchor(target, writeTagAndAnchor(target,
item.scalarTag, tagLib, item.scalarAnchor) item.scalarTag, tagLib, item.scalarAnchor)
@ -218,7 +213,7 @@ proc present*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
safeWrite(item.scalarContent) safeWrite(item.scalarContent)
of yamlAlias: of yamlAlias:
assert levels.len > 0 assert levels.len > 0
startItem(target, style, indentation, levels[levels.high]) startItem(target, style, indentation, levels[levels.high], false)
try: try:
target.write('*') target.write('*')
target.write(cast[byte]('a') + cast[byte](item.aliasTarget)) target.write(cast[byte]('a') + cast[byte](item.aliasTarget))
@ -254,7 +249,7 @@ proc present*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
dBlockSequenceItem dBlockSequenceItem
of ypsJson: of ypsJson:
if levels.len > 0 and levels[levels.high] in if levels.len > 0 and levels[levels.high] in
[dFlowImplicitMapStart, dFlowImplicitMapValue]: [dFlowMapStart, dFlowMapValue]:
raise newException(YamlPresenterJsonError, raise newException(YamlPresenterJsonError,
"Cannot have sequence as map key in JSON output!") "Cannot have sequence as map key in JSON output!")
nextState = dFlowSequenceStart nextState = dFlowSequenceStart
@ -275,7 +270,7 @@ proc present*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
safeWrite('\x0A') safeWrite('\x0A')
indentation += indentationStep indentation += indentationStep
else: else:
startItem(target, style, indentation, levels[levels.high]) startItem(target, style, indentation, levels[levels.high], true)
if style != ypsJson: if style != ypsJson:
writeTagAndAnchor(target, writeTagAndAnchor(target,
item.seqTag, tagLib, item.seqAnchor) item.seqTag, tagLib, item.seqAnchor)
@ -285,9 +280,8 @@ proc present*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
safeWrite('[') safeWrite('[')
if levels.len > 0 and style in [ypsJson, ypsCanonical] and if levels.len > 0 and style in [ypsJson, ypsCanonical] and
levels[levels.high] in levels[levels.high] in
[dBlockExplicitMapKey, dBlockExplicitMapValue, [dBlockExplicitMapKey, dBlockMapValue,
dBlockImplicitMapKey, dBlockImplicitMapValue, dBlockImplicitMapKey, dBlockSequenceItem]:
dBlockSequenceItem]:
indentation += indentationStep indentation += indentationStep
levels.add(nextState) levels.add(nextState)
of yamlStartMap: of yamlStartMap:
@ -314,27 +308,21 @@ proc present*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
var e = newException(YamlPresenterStreamError, "") var e = newException(YamlPresenterStreamError, "")
e.cause = getCurrentException() e.cause = getCurrentException()
raise e raise e
nextState = if length <= 60: dFlowImplicitMapStart else: nextState = if length <= 60: dFlowMapStart else: dBlockMapValue
if item.mapMayHaveKeyObjects:
dBlockExplicitMapValue else: dBlockImplicitMapValue
of ypsMinimal: of ypsMinimal:
nextState = if item.mapMayHaveKeyObjects: nextState = dFlowMapStart
dFlowExplicitMapStart else: dFlowImplicitMapStart
of ypsCanonical: of ypsCanonical:
nextState = dFlowExplicitMapStart nextState = dFlowMapStart
of ypsJson: of ypsJson:
if levels.len > 0 and levels[levels.high] in if levels.len > 0 and levels[levels.high] in
[dFlowImplicitMapStart, dFlowImplicitMapValue]: [dFlowMapStart, dFlowMapValue]:
raise newException(YamlPresenterJsonError, raise newException(YamlPresenterJsonError,
"Cannot have map as map key in JSON output!") "Cannot have map as map key in JSON output!")
nextState = dFlowImplicitMapStart nextState = dFlowMapStart
of ypsBlockOnly: of ypsBlockOnly:
nextState = if item.mapMayHaveKeyObjects: nextState = dBlockMapValue
dBlockExplicitMapValue else: dBlockImplicitMapValue
if levels.len == 0: if levels.len == 0:
if nextState in if nextState == dBlockMapValue:
[dBlockExplicitMapValue, dBlockImplicitMapValue]:
if style != ypsJson: if style != ypsJson:
writeTagAndAnchor(target, writeTagAndAnchor(target,
item.mapTag, tagLib, item.mapAnchor) item.mapTag, tagLib, item.mapAnchor)
@ -345,27 +333,26 @@ proc present*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
item.mapTag, tagLib, item.mapAnchor) item.mapTag, tagLib, item.mapAnchor)
indentation += indentationStep indentation += indentationStep
else: else:
if nextState in if nextState in [dBlockMapValue, dBlockImplicitMapKey]:
[dBlockExplicitMapValue, dBlockImplicitMapValue,
dBlockImplicitMapKey]:
if style != ypsJson: if style != ypsJson:
writeTagAndAnchor(target, writeTagAndAnchor(target,
item.mapTag, tagLib, item.mapAnchor) item.mapTag, tagLib, item.mapAnchor)
startItem(target, style, indentation, levels[levels.high]) startItem(target, style, indentation, levels[levels.high],
true)
else: else:
startItem(target, style, indentation, levels[levels.high]) startItem(target, style, indentation, levels[levels.high],
true)
if style != ypsJson: if style != ypsJson:
writeTagAndAnchor(target, writeTagAndAnchor(target,
item.mapTag, tagLib, item.mapAnchor) item.mapTag, tagLib, item.mapAnchor)
indentation += indentationStep indentation += indentationStep
if nextState in [dFlowImplicitMapStart, dFlowExplicitMapStart]: if nextState == dFlowMapStart:
safeWrite('{') safeWrite('{')
if levels.len > 0 and style in [ypsJson, ypsCanonical] and if levels.len > 0 and style in [ypsJson, ypsCanonical] and
levels[levels.high] in levels[levels.high] in
[dBlockExplicitMapKey, dBlockExplicitMapValue, [dBlockExplicitMapKey, dBlockMapValue,
dBlockImplicitMapKey, dBlockImplicitMapValue, dBlockImplicitMapKey, dBlockSequenceItem]:
dBlockSequenceItem]:
indentation += indentationStep indentation += indentationStep
levels.add(nextState) levels.add(nextState)
@ -387,16 +374,14 @@ proc present*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
e.cause = getCurrentException() e.cause = getCurrentException()
raise e raise e
if levels.len == 0 or levels[levels.high] notin if levels.len == 0 or levels[levels.high] notin
[dBlockExplicitMapKey, dBlockExplicitMapValue, [dBlockExplicitMapKey, dBlockMapValue,
dBlockImplicitMapKey, dBlockImplicitMapValue, dBlockImplicitMapKey, dBlockSequenceItem]:
dBlockSequenceItem]:
continue continue
of dFlowSequenceStart: of dFlowSequenceStart:
if levels.len > 0 and style in [ypsJson, ypsCanonical] and if levels.len > 0 and style in [ypsJson, ypsCanonical] and
levels[levels.high] in levels[levels.high] in
[dBlockExplicitMapKey, dBlockExplicitMapValue, [dBlockExplicitMapKey, dBlockMapValue,
dBlockImplicitMapKey, dBlockImplicitMapValue, dBlockImplicitMapKey, dBlockSequenceItem]:
dBlockSequenceItem]:
indentation -= indentationStep indentation -= indentationStep
safeWrite(']') safeWrite(']')
of dBlockSequenceItem: of dBlockSequenceItem:
@ -407,7 +392,7 @@ proc present*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
of yamlEndMap: of yamlEndMap:
assert levels.len > 0 assert levels.len > 0
case levels.pop() case levels.pop()
of dFlowImplicitMapValue, dFlowExplicitMapValue: of dFlowMapValue:
case style case style
of ypsDefault, ypsMinimal, ypsBlockOnly: of ypsDefault, ypsMinimal, ypsBlockOnly:
safeWrite('}') safeWrite('}')
@ -422,19 +407,17 @@ proc present*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
e.cause = getCurrentException() e.cause = getCurrentException()
raise e raise e
if levels.len == 0 or levels[levels.high] notin if levels.len == 0 or levels[levels.high] notin
[dBlockExplicitMapKey, dBlockExplicitMapValue, [dBlockExplicitMapKey, dBlockMapValue,
dBlockImplicitMapKey, dBlockImplicitMapValue, dBlockImplicitMapKey, dBlockSequenceItem]:
dBlockSequenceItem]:
continue continue
of dFlowImplicitMapStart, dFlowExplicitMapStart: of dFlowMapStart:
if levels.len > 0 and style in [ypsJson, ypsCanonical] and if levels.len > 0 and style in [ypsJson, ypsCanonical] and
levels[levels.high] in levels[levels.high] in
[dBlockExplicitMapKey, dBlockExplicitMapValue, [dBlockExplicitMapKey, dBlockMapValue,
dBlockImplicitMapKey, dBlockImplicitMapValue, dBlockImplicitMapKey, dBlockSequenceItem]:
dBlockSequenceItem]:
indentation -= indentationStep indentation -= indentationStep
safeWrite('}') safeWrite('}')
of dBlockImplicitMapValue, dBlockExplicitMapValue: of dBlockMapValue:
discard discard
else: else:
assert false assert false

View File

@ -82,19 +82,12 @@ type
## ``yTagQuestionMark`` and ``yTagExclamationMark`` respectively. ## ``yTagQuestionMark`` and ``yTagExclamationMark`` respectively.
## Mapping is done by a `YamlTagLibrary <#YamlTagLibrary>`_. ## Mapping is done by a `YamlTagLibrary <#YamlTagLibrary>`_.
## ##
## The value ``mapMayHaveKeyObjects`` is a hint from a serializer and is
## used for choosing an appropriate presentation mode for a YAML map
## (flow or block, explicit or implicit) by
## `present <#present,YamlStream,Stream,YamlTagLibrary,YamlDumpStyle,int>`_.
## If it is set to ``false``, the map may only have scalars as keys.
##
## The value ``scalarType`` is a hint from the lexer, see ## The value ``scalarType`` is a hint from the lexer, see
## `YamlTypeHint <#YamlTypeHint>`_. ## `YamlTypeHint <#YamlTypeHint>`_.
case kind*: YamlStreamEventKind case kind*: YamlStreamEventKind
of yamlStartMap: of yamlStartMap:
mapAnchor* : AnchorId mapAnchor* : AnchorId
mapTag* : TagId mapTag* : TagId
mapMayHaveKeyObjects* : bool
of yamlStartSequence: of yamlStartSequence:
seqAnchor* : AnchorId seqAnchor* : AnchorId
seqTag* : TagId seqTag* : TagId
@ -310,8 +303,7 @@ proc `$`*(event: YamlStreamEvent): string
proc startDocEvent*(): YamlStreamEvent {.inline.} proc startDocEvent*(): YamlStreamEvent {.inline.}
proc endDocEvent*(): YamlStreamEvent {.inline.} proc endDocEvent*(): YamlStreamEvent {.inline.}
proc startMapEvent*(tag: TagId = yTagQuestionMark, proc startMapEvent*(tag: TagId = yTagQuestionMark,
anchor: AnchorId = yAnchorNone, anchor: AnchorId = yAnchorNone): YamlStreamEvent {.inline.}
mayHaveKeyObjects: bool = true): YamlStreamEvent {.inline.}
proc endMapEvent*(): YamlStreamEvent {.inline.} proc endMapEvent*(): YamlStreamEvent {.inline.}
proc startSeqEvent*(tag: TagId = yTagQuestionMark, proc startSeqEvent*(tag: TagId = yTagQuestionMark,
anchor: AnchorId = yAnchorNone): YamlStreamEvent {.inline.} anchor: AnchorId = yAnchorNone): YamlStreamEvent {.inline.}