cue icon indicating copy to clipboard operation
cue copied to clipboard

encoding/yaml: Support for encoding tags

Open uhthomas opened this issue 2 years ago • 3 comments

Is your feature request related to a problem? Please describe.

There are some situations where a yaml tag is required. (encoding/yaml).Marshal does not support encoding tags and makes this impossible.

It looks like the authors of the original encoder were aware this may be desirable.

https://github.com/cue-lang/cue/blob/256f1f9ee1611636fef85446ff9ea3b7d4f6b7d0/internal/encoding/yaml/encode.go#L107

Describe the solution you'd like

Support encoding tags with an attribute.

import "encoding/yaml"

yaml.Marshal({
    some_key: "some value" @yaml.Tag("some tag")
})

Describe alternatives you've considered Not sure.

Additional context

Maybe:

  • https://github.com/cue-lang/cue/issues/422
  • https://github.com/cue-lang/cue/issues/1070

uhthomas avatar Mar 27 '23 10:03 uhthomas

TIL about YAML tags.

Thanks for raising.

myitcv avatar Apr 06 '23 12:04 myitcv

This issue affects me. I'm using Material for MkDocs to write some documentation, and its YAML config file makes occasional use of YAML tags - meaning that I can't use (only) CUE to encode it, if I want to access certain features.

For example, this YAML enables nicer anchors for content tabs:

markdown_extensions:
  - pymdownx.tabbed:
      slugify: !!python/object/apply:pymdownx.slugs.slugify
        kwds:
          case: lower

jpluscplusm avatar Jul 16 '25 08:07 jpluscplusm

I’m interested in helping move this issue forward, even though it’s a few years old, because I’ve run into a concrete use case for YAML tags.

My use case is Authentik blueprints, which rely on custom YAML tags like !Find, !KeyOf, !If, etc. At the moment, CUE can describe the structure of these blueprints nicely, but there’s no way to emit the actual tags when encoding to YAML, so I have to post-process the output.

Proposed behaviour

I’d like to propose adding a simple attribute for this, along the lines of:

someField: "value" @yaml(tag="!Find")

which would encode to YAML as:

someField: !Find value

Some notes/constraints:

  • The @yaml(tag="…") attribute would be purely an encoding hint:

    • It affects cue export --out yaml and encoding/yaml.Marshal{,Stream}.
    • It does not change CUE evaluation semantics.
  • It should be applicable to scalars, lists, and structs (tagging the whole YAML node).

  • Other encoders (JSON, etc.) would ignore the tag attribute.

Willing to implement

If this direction still fits with CUE’s plans, I’d be happy to:

  • Add tag support in the internal YAML encoder (mapping @yaml(tag="...") to yaml.Node.Tag),
  • Wire it through the encoding/yaml package,
  • And include tests + docs/examples (e.g. using Authentik-style tags) as part of the change.

Before I start on a patch: Is this attribute design acceptable, or would you prefer a different shape (e.g. @yaml.Tag("!Find") or something else)? And is this feature still in scope for the project?

Thanks!

jonas-meyer avatar Nov 26 '25 12:11 jonas-meyer