libfyaml icon indicating copy to clipboard operation
libfyaml copied to clipboard

sequence of documents output style

Open cruisercoder opened this issue 2 years ago • 1 comments

I'm trying to get the emitter to output an array of documents in full block style. This is what the library outputs in block mode:

key: [
    a: 1,
    b: 2]

and this is what I'm trying to emit as output:

key:
  - a: 1
  - b: 2

Is this possible?

cruisercoder avatar Sep 29 '22 01:09 cruisercoder

it has something do internally with an empty save ctx in fy_emit_sequence_prolog.

The program below shows the issue. This is the output:

key: [
    a: 1]

struct fy_emitter*, enum fy_emitter_write_type, const char* str, int len, void*) {
  write(1, str, len);
  return len;
}

int main(int argc, char* argv[]) {
  struct fy_emitter_cfg cfg;
  struct fy_emitter* emit;
  cfg.flags = FYECF_MODE_BLOCK;
  cfg.output = handler;
  emit = fy_emitter_create(&cfg);
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_STREAM_START));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_DOCUMENT_START, true, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_MAPPING_START, FYNS_BLOCK, NULL, NULL));
  fy_emit_event(emit,
                fy_emit_event_create(emit, FYET_SCALAR, FYSS_PLAIN, "key", FY_NT, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_SEQUENCE_START, FYNS_BLOCK, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_MAPPING_START, FYNS_BLOCK, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_SCALAR, FYSS_PLAIN, "a", FY_NT, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_SCALAR, FYSS_PLAIN, "1", FY_NT, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_MAPPING_END));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_SEQUENCE_END));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_MAPPING_END));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_DOCUMENT_END, true, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_STREAM_END));
  fy_emitter_destroy(emit);
}

cruisercoder avatar Oct 01 '22 12:10 cruisercoder

I am currently testing a fix - the problem is somewhat tricky because you're using the event interface directly and the style hints were not respected in all of the cases.

pantoniou avatar Jan 11 '23 09:01 pantoniou

01953f31f7c219061d17beba59c631620a967ad3 should fix this.

You will need to use the manual emitting mode to make sure the hints are respected. If this fixes your issue, please close.

pantoniou avatar Jan 29 '23 17:01 pantoniou

This seems to fix the issue for me. Is there a way, however, to allow indentation of the dash? There seems to be agreement that this is easier to read. The output now, which is valid YAML, is this:

key:
- a: 1
- b: 2

This is the output I would like to see, which is also valid but more readable, is this:

key:
  - a: 1
  - b: 2

cruisercoder avatar Mar 21 '23 18:03 cruisercoder

The original issue is fixed; Reopen this for styling options

pantoniou avatar Oct 21 '23 17:10 pantoniou