Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Dump of custom type #364

Closed
elorzafe opened this issue Aug 18, 2017 · 4 comments
Closed

Dump of custom type #364

elorzafe opened this issue Aug 18, 2017 · 4 comments

Comments

@elorzafe
Copy link

Thanks you for this library, I have found it a couple of days ago and is very helpful for me 👍

The issue I have is with dump when I have custom types

the file custom_types.yml has defined !space and !point type but when I try to dump it, I get !<!space> and !<!point> instead.

There is a way to dump the original file?

Thanks again!

@hectorguo
Copy link

I have the same issue.
Is there a way to get !space and !point when dumping?

@hectorguo
Copy link

found the related discussion: #187

@pawel-kedzior-sw
Copy link

There are two issues here.

One is a bug in how the options are interpreted. There is a difference in how they are treated when loading and dumping. Second is that when dumping, the tags are always wrapped in < and >.

Here is an example (typescript):

   let types : jsyaml.Type[] = []

    let options : jsyaml.TypeConstructorOptions = {
      kind: 'mapping',
      construct: function (data : any) {
        if (data == null) {
          data = {};
        }
        data._type = "circle"
        return data;         
      },
      predicate: function (data: any): boolean {
        if (data._type == "circle") {
          return true;
        }
        return false;
      },
      represent: function (data: object): any {
        delete (data as any)._type;
        return data;
      }
    };

    types.push(new jsyaml.Type("!circle", options));

    let schema = jsyaml.Schema.create(types);

    let yaml = 
        "shape: !circle\r\n"+
        "   radius: 5\r\n"
      
    let diagram = jsyaml.load(yaml, { schema: schema });
    let output = jsyaml.dump(diagram, { schema: schema })

    console.log(output);

and as of now, the output is:

shape: !<!circle> 
  radius: 5

It is possible to work-around the first problem by building different options for loading:
new jsyaml.Type("!circle", options)
and different for dumping:
new jsyaml.Type("circle", options)

but I do not see an option to workaround the problem of unneeded < and > characters.

@rlidwka
Copy link
Member

rlidwka commented Dec 25, 2020

Fixed in a0d0caa (currently in dev branch).

Now Type('!circle') is dumped as !circle, and Type('circle') is dumped as !<circle>. Same as in pyyaml, there is no way to write !<!circle> anymore. Please tell me if there's any use-case for the old behavior.

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

No branches or pull requests

4 participants