Skip to content
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

dumping is formating custom type wrong #576

Closed
rubendmatos1985 opened this issue Aug 29, 2020 · 4 comments
Closed

dumping is formating custom type wrong #576

rubendmatos1985 opened this issue Aug 29, 2020 · 4 comments

Comments

@rubendmatos1985
Copy link

rubendmatos1985 commented Aug 29, 2020

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
  

*/

@rlidwka
Copy link
Member

rlidwka commented Sep 15, 2020

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)?

@rubendmatos1985
Copy link
Author

rubendmatos1985 commented Sep 18, 2020

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

@danmactough
Copy link

+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?

@rlidwka
Copy link
Member

rlidwka commented Dec 25, 2020

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

@rlidwka rlidwka closed this as completed Dec 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants