Git Product home page Git Product logo

Comments (12)

k8s-ci-robot avatar k8s-ci-robot commented on August 25, 2024

There are no sig labels on this issue. Please add an appropriate label by using one of the following commands:

  • /sig <group-name>
  • /wg <group-name>
  • /committee <group-name>

Please see the group list for a listing of the SIGs, working groups, and committees available.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

from kubernetes.

k8s-ci-robot avatar k8s-ci-robot commented on August 25, 2024

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

from kubernetes.

cyclinder avatar cyclinder commented on August 25, 2024

how about sorting by lastTransitionTime?

from kubernetes.

thockin avatar thockin commented on August 25, 2024

I didn't mean sorting the conditions, I meant sorting the fields within each condition so that type is always first. But not just conditions. This is just one example though. So many of our structures have fields that should be first, like name or key, but they aren't first alphabetically. When I look at emitted yaml or json. It's just not in an order that I naturally want to see it, which makes examples and CLI output less friendly than it could be

from kubernetes.

cyclinder avatar cyclinder commented on August 25, 2024

ok, I got it, that makes sense, I think the conditions also should be sorted. Do you agree with this?

/assign @cyclinder

from kubernetes.

thockin avatar thockin commented on August 25, 2024

I think the conditions also should be sorted.

Conditions are a sort of extension point - we don't know every possible Condition. Sorting them based on transition time could confuse clients who look at the YAML/JSON for diffs.

From a UX point of view, I am much more concerned with YAML/JSON readability wrt the fields we know exist. I don't think Go's standard JSON supports any sort of thing here, but something like keeping the field ordering when rendering is what I was thinking

from kubernetes.

cyclinder avatar cyclinder commented on August 25, 2024

OK, the reason for sorting conditions by lastTransitionTime is that the user can know what's happening based on the timeline, which may help the user understand the entire startup process of the pod. If you think you're worried about something else, let's focus on sorting the fields first.

from kubernetes.

brianpursley avatar brianpursley commented on August 25, 2024

I believe this happens because of how json.Marshal is implemented.

Could this be where the alphabetical field sorting is being done?
https://github.com/golang/go/blob/0253c0f4acdf9f7a2930609f55d24b334fa7d3c2/src/encoding/json/encode.go#L1186-L1203

	slices.SortFunc(fields, func(a, b field) int {
		// sort field by name, breaking ties with depth, then
		// breaking ties with "name came from json tag", then
		// breaking ties with index sequence.
		if c := strings.Compare(a.name, b.name); c != 0 {
			return c
		}
		if c := cmp.Compare(len(a.index), len(b.index)); c != 0 {
			return c
		}
		if a.tag != b.tag {
			if a.tag {
				return -1
			}
			return +1
		}
		return slices.Compare(a.index, b.index)
	})

Maybe this could be extended to optionally support the specification of an order in the struct field's json tag...

from kubernetes.

thockin avatar thockin commented on August 25, 2024

Note that we emit both JSON and YAML (and maybe others but they matter less?)

from kubernetes.

brianpursley avatar brianpursley commented on August 25, 2024

Yeah, it could be a problem in both places, but the yaml printer uses the same json.Marshal and then converts the json to yaml as a second step.

from kubernetes.

brianpursley avatar brianpursley commented on August 25, 2024

Actually it is in kubernetes' yaml package (used by the printer) where it first marshals to JSON, then converts to YAML.

// Marshal marshals obj into JSON using stdlib json.Marshal, and then converts JSON to YAML using JSONToYAML (see that method for more reference)
func Marshal(obj interface{}) ([]byte, error) {
jsonBytes, err := json.Marshal(obj)
if err != nil {
return nil, fmt.Errorf("error marshaling into JSON: %w", err)
}
return JSONToYAML(jsonBytes)
}

from kubernetes.

brianpursley avatar brianpursley commented on August 25, 2024

I did a test and I think I was wrong. json.MarshalIndent and json.Marshal seem to use whatever field order you define in the struct.

Example:

package main

import "encoding/json"

type Example struct {
	Foo string `json:"foo"`
	Bar int    `json:"bar"`
}

func main() {
	examples := []Example{
		{"Test1", 1},
		{"Test2", 2},
		{"Test3", 3},
	}

	result, err := json.MarshalIndent(examples, "", "  ")
	if err != nil {
		panic(err)
	}

	println(string(result))
}

Output:

[
  {
    "foo": "Test1",
    "bar": 1
  },
  {
    "foo": "Test2",
    "bar": 2
  },
  {
    "foo": "Test3",
    "bar": 3
  }
]

Go version 1.23.0

I guess I will dig a little deeper... 👀


Ah, further down in the code, it sorts the fields again, this time by index, which I guess is what produces the output in the field order.

https://github.com/golang/go/blob/0253c0f4acdf9f7a2930609f55d24b334fa7d3c2/src/encoding/json/encode.go#L1234-L1236

	slices.SortFunc(fields, func(i, j field) int {
		return slices.Compare(i.index, j.index)
	})

So I guess there has to be something in kubectl that is causing the fields to show up alphabetically. Maybe it is unmarshaled into a map at some point...

from kubernetes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.