Skip to content

Preserve object key ordering when generating output #222

@bradrydzewski

Description

@bradrydzewski

👋 Hi there, similar to ksonnet, there is interest in the drone community to use jsonnet to simplify pipeline configuration files. Our community is building increasingly complex yaml files like this that could really benefit from jsonnet.

For reference, this is a simple example of a yaml pipeline configuration that defines two pipeline steps, executed sequentially:

pipeline:
  frontend:
    image: node
    commands:
      - npm install
      - npm test
  backend:
    image: golang
    commands:
      - go build
      - go test

Ideally a jsonnet representation would directly mirror the yaml structure, giving developers the ability to more easily migrate from yaml to jsonnet. For example:

{
  frontend: {
    image: "node",
    commands: [
      "npm install",
      "npm test"
     ]
  },
  backend: {
    image: "golang",
    commands: [
      "go build",
      "go test"
     ]
  }
}

The challenge we face is that jsonnet orders keys alphabetically, which means we cannot rely on the ordering of the pipeline steps. Changing our yaml pipeline structure to use a slice instead of a map is unfortunately not an option for us, since it would break a large number of our existing installations.

I was therefore wondering if the community would be open to (optionally) retaining order, perhaps in a manner similar to go-yaml. The go-yaml package implements custom types that retain order when unmarshaled and marshaled:

I do apologize if it seems forward to ask a go-jsonnet to add complexity to its implementation to support the a single project/community, and I certainly understand if such a change/customization is not desired. If there is interest, however, I would happily volunteer to author such a change. Thanks for the consideration!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions