SharpYaml icon indicating copy to clipboard operation
SharpYaml copied to clipboard

Can I Read/Write Anchor Names Somehow?

Open andrejohansson opened this issue 8 years ago • 3 comments

I want to be able to control the names of anchors being emitted, more specifically:

  1. How do I control the output name of an anchor?
  2. How can I get the anchor name after dezerialisation?
public class MyNode
{
  public string Name { get; set; }
  public string  MyScalar { get; set; }
}

public class MyNodeList
{
  public IList<MyNode> Items { get; set; }
}

var node = new MyNode 
{
  [YamlAnchor] // this attribute doesn´t exist, but is there something similar?
  Name = "i_want_this_to_be_the_anchor_name",
  MyScalar = "hello"
};

var list = new MyNodeList() {
  node,
  node,
  node,
  node
}

How can I get this:

MyNode:
  - &i_want_this_to_be_the_anchor_name
    Name: some_name
    MyScalar: hello

Items:
  - *i_want_this_to_be_the_anchor_name
  - *i_want_this_to_be_the_anchor_name
  - *i_want_this_to_be_the_anchor_name
  - *i_want_this_to_be_the_anchor_name

instead of this:

MyNode:
  - &o2
    Name: some_name
    MyScalar: hello

Items:
  - *o2
  - *o2
  - *o2
  - *o2

andrejohansson avatar Oct 03 '17 11:10 andrejohansson

I barely invested in alias handling as it was a niche feature (I'm not saying it's not useful) so this might require a few changes in the code to support this scenario (feel free to PR). Note also that as I explained in #28, aliasing is partially implemented, supporting only forward but not backward aliasing, that might not work well in write-re-read scenarios...

xoofx avatar Oct 07 '17 18:10 xoofx

Ok, then I know! Any pointers to where in the code I might start are appreciated. I might do a pull request later depending on where we land with this.

Thanks for your effort with this library, very useful!

andrejohansson avatar Oct 09 '17 08:10 andrejohansson

This is mostly handled in AnchorSerializer.cs. For the problem described in #28 (backward reference), that might require more work than just adapting the AnchorSerializer

xoofx avatar Oct 09 '17 08:10 xoofx