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

Add JSONObjectToYAMLObject #14

Merged
merged 1 commit into from
Jun 26, 2019

Conversation

sttts
Copy link

@sttts sttts commented Feb 28, 2019

JSONObjectToYAMLObject is used to convert to a YAML MapSlice representation without going through a byte slice representation, which is too heavy on allocations for some applications.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Feb 28, 2019
@k8s-ci-robot k8s-ci-robot added the sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. label Feb 28, 2019
@sttts
Copy link
Author

sttts commented Feb 28, 2019

/assign @apelisse

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Feb 28, 2019
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Feb 28, 2019
yaml_test.go Show resolved Hide resolved
yaml.go Outdated Show resolved Hide resolved
yaml_test.go Outdated
"map": map[string]interface{}{"foo": "bar"},
"slice": []interface{}{"foo", "bar"},
"string": string("foo"),
"uint64 big": float64(math.Pow(2, 63)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{"big negative", float64(math.Pow(-2, 63)}
will resolve to:
{"big negative", int(-9223372036854775808)}
on 64bit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also what happens if we pass a big int64(x) value on a 32bit word size?
would the value be truncated by translating to int()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-float64(math.Pow(2, 63) round trips to a float64 non-precise number with yaml.Unmarshal(json.Marshal(x)) although it fits into int64.

@dims
Copy link
Member

dims commented Feb 28, 2019

/assign @liggitt @neolit123

yaml.go Show resolved Hide resolved
@sttts
Copy link
Author

sttts commented Apr 2, 2019

@liggitt @apelisse addressed comments.

// string, bool and any other types are unchanged.
func JSONObjectToYAMLObject(j map[string]interface{}) yaml.MapSlice {
if len(j) == 0 {
return nil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is surprising... doesn't this mean that an empty map doesn't round trip? if so, doesn't that break uses like CRD status (status: {})

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is what the yaml library does, compare the tests.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... it's still not quite clear to me who is intending to call this helper method, and whether this len(0) => nil treatment is going to bite us... any chance we can fix the yaml library instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that this is just the top-level conversion func. Further down we have jsonToYAMLValue which is faithful with empty maps.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liggitt actually this semantics is not that surprising and nothing to change:

A yaml in-memory data structure has interface{}{nil} for null values. yaml.MapSlice(nil) is not null, but the empty map. It marshals to {}.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok

@errordeveloper
Copy link

Is this going in any time soon?

@dims
Copy link
Member

dims commented Apr 26, 2019

@liggitt can you please look over the responses from @sttts ?

@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sttts

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 24, 2019
@sttts sttts force-pushed the sttts-json-to-yaml branch 2 times, most recently from 2d3a4e1 to 989bfe4 Compare June 25, 2019 10:24
@liggitt
Copy link

liggitt commented Jun 26, 2019

/lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants