Skip to content

dumping is formating custom type wrong #576

Closed

Description

@coderwaschbaer
const yaml = require("js-yaml")

class Test {
  constructor(id) {
    this.id = id
  }
  id

  get Id() {
    return this.id
  }
  async fetchData() {
    console.log("get async data")
  }
}



var customtagYamlType = new yaml.Type("!mapInfo", {
  kind: "scalar",
  resolve: () => true,
  construct: (id) => new Test(id),
  instanceOf: Test,
  represent: (customTag) => customTag.id,
})

const obj = {
  name: 'some',
  data:{
    content:[
      {
        value: new Test("1")
      }
    ]
  }
}


const schema = yaml.Schema.create([customtagYamlType])

const result = yaml.dump(obj, { schema })
console.log(result)

/* the output is 
data:
  content:
  - value: !<!mapInfo> '1'

-----------------------------------------
i am expecting like in the documentation
data:
  content:
  - value: !mapInfo  '1'



node version v12.14.1
  

*/

Activity

  1. rlidwka commented on Sep 15, 2020

    @rlidwka
    Member

    Those string representations are equivalent:

    console.log(yaml.load(`
    a: !<!mapInfo> '1'
    b: !mapInfo '1'
    `, { schema }))
    
    // prints { a: Test { id: '1' }, b: Test { id: '1' } }

    !<!mapInfo> is a global version of the application-local tag !mapInfo, both variants are acceptable I believe.

    Does it cause any issues (e.g. interoperability with other parsers)?

  2. coderwaschbaer commented on Sep 18, 2020

    @coderwaschbaer
    Author

    Yes they are equivalent but the same library throw an error when convert from !mapInfo to !<!mapInfo> and i try to convert again to json. My example to get in context is that i am making an app which convert Yaml to Json shows a UI where you can edit the content and after that will convert the json again to YAML. I have to use string.replace('!<!mapInfo>', '!mapInfo' ) after dumping. It is working fine but if the object is to long with a lot of custom tags i will compromise the perfromance of the app

  3. danmactough commented on Oct 4, 2020

    @danmactough

    +1 I am also working on a library where I want to be able to load yaml with custom tags, process/transform the data, and then dump the transformed data as yaml while preserving the original -- I want to be able to perform a diff and don't want every custom tag rewritten.

    If we had an option available when creating a Type (for example, a boolean flag -- dumpTagsWithGlobalWrapper), it seems like here, we could propagate that option to the state with something like state.dumpTagsWithGlobalWrapper = !!type.dumpTagsWithGlobalWrapper. Then where we currently add that wrapper here, we could conditionally add the wrapper or not.

    Would you accept a PR for this?

  4. rlidwka commented on Dec 25, 2020

    @rlidwka
    Member

    Fixed in a0d0caa (currently in dev branch).

    Now tag behavior aligns with pyyaml, where custom tags are dumped as !tag, and there is no way to dump those as !<!tag>. Please tell me if there's any use-case for the old behavior.

    Duplicate of:
    #364
    #187

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions