Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Custom sort order #2012

Open
Zash opened this issue Apr 15, 2024 · 0 comments
Open

Custom sort order #2012

Zash opened this issue Apr 15, 2024 · 0 comments

Comments

@Zash
Copy link
Contributor

Zash commented Apr 15, 2024

Please describe your feature request.
I wish I could use yq to sort 'keywords' in JSON Schema files by a certain order, i.e. title and description first. Sorting alphabetically with sort_keys(..) puts title after many other common keywords, which make schemas less usable as documentation when read as-is.

My idea for syntax would be to extend the sort_keys() function with an argument that describes the order one wants, here imagined as an array with keys in the wished order. Keys not part of that array could go in alphabetical order after those. The argument could also be some keyword referencing an ordering function, that defaults to alphabetic. I assume passing a whole function would be out of scope for yq.

Describe the solution you'd like
If we have schema1.yml like:
(please keep to around 10 lines )

additionalProperties: false
description: |
  This schema describes the foo-bar thing which consists of a string foo and a
  number bar.
properties:
  bar:
    title: and the bar
    type: number
  foo:
    title: the foo
    type: string
title: An example schema
type: object

And we run a command:

yq -P -i 'sort_keys(.., ["title", "description", "type", "properties", "additionalProperties"])' schema1.yaml

it could output

title: An example schema
description: |
  This schema describes the foo-bar thing which consists of a string foo and a
  number bar.
type: object
properties:
  foo:
    title: the foo
    type: string
  bar:
    title: and the bar
    type: number
additionalProperties: false

Describe alternatives you've considered

  • Doing it manually.
  • Writing some sort of dedicated tool for this job.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant