flyx / NimYAML

YAML implementation for Nim
https://nimyaml.org
Other
191 stars 36 forks source link

Ignore adding %TAG to yaml file #95

Closed dmknght closed 3 years ago

dmknght commented 3 years ago

Nim version: 1.4.2 Debian Yaml data image I'm trying read the data from config file then write data to understand how the code work

import os
import yaml / [serialization, presenter]
import streams

let configFile = getHomeDir() & ".faraday-cli.yml"

type
  AuthNode = object
    faraday_url: string
    ignore_ssl: bool
    token: string
  FaradayNode = object
    workspace: string
  SettingsNode = object
    custom_plugins_folder: string
  Config = object
    auth: AuthNode
    faraday: FaradayNode
    settings: SettingsNode

var config* = Config()

proc load_config*(config: var Config) =
  if fileExists(config_file):
    var
      fileStream: FileStream
    try:
      fileStream = newFileStream(config_file)
      load(fileStream, config)
    except:
      discard
    finally:
      fileStream.close()
  discard

proc save_config*(config: Config) =
  var
    fileStream: FileStream
  try:
    fileStream = newFileStream(config_file, fmWrite)
    # dump(config, fileStream, tagStyle = tsNone, options = defineOptions(style = psDefault, outputVersion = ovNone))
    dump(config, fileStream, options = defineOptions(outputVersion = ovNone))
  except:
    discard
  finally:
    fileStream.close()

load_config(config)
save_config(config)

The saved yaml file has

%TAG !n! tag:nimyaml.org,2016:
--- !n!custom:Config 

image

With tagStyle tsNone, i have saved file with

%TAG !n! tag:nimyaml.org,2016:
--- 

Function call: dump(config, fileStream, tagStyle = tsNone, options = defineOptions(outputVersion = ovNone)) image From https://github.com/flyx/NimYAML/blob/03ecab0075eccd94f78d347d9c68ccd6f6999d1f/yaml/presenter.nim#L480, I can see the code add data if psStyle != psJson. I can't see any option from comments or variables that can disable this.

flyx commented 3 years ago

The problem is here, where representing anything as a document adds the !n! shortcut to the document start.

It baffles me a bit that no-one complained about this until now. For all I can remember, this was an early hack to ensure that if the serialization generates NimYAML-specific tags, I will have the prefix available, and I never reworked it to do something more intelligent. The most obvious problem is that tsNone doesn't lead to exclusion of this prefix. But more generally, the tag shouldn't be included if it isn't used anywhere, for example if you use tsRootOnly and have a custom tag defined for your root type (via setTagUri).

I will go and review the tag handling a bit more since I wrote that stuff five years ago, then decide on how to fix this.

flyx commented 3 years ago

You can give handles = @[] now if you don't want any %TAG directive in the output. Sadly, tsNone is not sufficient for dropping the directive since {. implicit .} types may still require a tag. Inspecting every element beforehand to see if one requires the handle is possible but I decided against implementing it since it would impact performance and it's better to just have the calling code being responsible for the used tag handles since that code should know what kind of tags will be produced by dumping the given value.

dmknght commented 3 years ago

@flyx Thank you for the commit. All unexpected data was removed but this --- image My function dump(config, fileStream, tagStyle = tsNone, handles = @[], options = defineOptions(outputVersion = ovNone)). Is there any option that allow me remove this as well? And just suggestion: Do you think add \n to end of file is a good idea?