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 Encoder and Decoder #59

Closed

Conversation

kevin-hanselman
Copy link

@kevin-hanselman kevin-hanselman commented Aug 14, 2021

A port of ghodss#39

Original code by @VictorLowther, adapted to this fork by me.

@k8s-ci-robot
Copy link

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


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/test-infra repository. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Aug 14, 2021
@k8s-ci-robot
Copy link

Welcome @kevin-hanselman!

It looks like this is your first PR to kubernetes-sigs/yaml 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/yaml has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added 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. labels Aug 14, 2021
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Aug 14, 2021
@dims
Copy link
Member

dims commented Aug 14, 2021

Hey @kevin-hanselman thanks for doing this. Could i persuade you to consider adding some benchmarks? (the linked PR hints at "memory footprint" gains ... so -benchmem?)

@dims
Copy link
Member

dims commented Aug 15, 2021

/assign @liggitt @deads2k

folks, can you please peek?

@kevin-hanselman
Copy link
Author

Added a (very) basic benchmark test. Here's the results on my machine:

This branch:

goos: linux
goarch: amd64
pkg: sigs.k8s.io/yaml
cpu: Intel(R) Core(TM) i3-7100 CPU @ 3.90GHz
BenchmarkMarshal-4        153171              7745 ns/op            9530 B/op         50 allocs/op
BenchmarkUnmarshal-4       36927             32184 ns/op           11413 B/op        151 allocs/op
PASS
ok      sigs.k8s.io/yaml        2.788s

master:

goos: linux
goarch: amd64
pkg: sigs.k8s.io/yaml
cpu: Intel(R) Core(TM) i3-7100 CPU @ 3.90GHz
BenchmarkMarshal-4        249537              4672 ns/op            8937 B/op         42 allocs/op
BenchmarkUnmarshal-4       65419             18461 ns/op           10891 B/op        141 allocs/op
PASS
ok      sigs.k8s.io/yaml        2.615s

I'll need to think more about these results and code before drawing any conclusions.

@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: kevin-hanselman
To complete the pull request process, please ask for approval from deads2k after the PR has been reviewed.

The full list of commands accepted by this bot can be found 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

@kevin-hanselman
Copy link
Author

@dims Anything else I can do to move this work along?

@dims
Copy link
Member

dims commented Sep 3, 2021

@deads2k @sttts @deads2k thoughts please!

cc @kubernetes-sigs/kubernetes-sig-api-machinery-reviewers

var intermediate interface{}
jsonEnc := json.NewEncoder(pipeWriter)
// We are using yaml.Decoder here (instead of json.Decoder) because the Go
// JSON library doesn't try to pick the right number type (int, float,
Copy link

Choose a reason for hiding this comment

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

json.NewDecoder(...).UseNumber() will preserve numeric input literally (https://pkg.go.dev/encoding/json#Decoder.UseNumber) ... would that enable translating with fidelity? how does the json decoder compare to the yaml decoder in efficiency?

Copy link

Choose a reason for hiding this comment

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

also, a unit test demonstrating some of the edge cases like this would be helpful in ensuring this isn't producing user-visible changes

other examples of weird edges in yaml encoding/decoding we don't want to accidentally modify behavior on (even if the current behavior is not ultimately desired/correct):

yamlEnc := yaml.NewEncoder(e.w)

var jsonErr error
go func() {
Copy link

Choose a reason for hiding this comment

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

spawning goroutines here and in Decode is a little hard to reason about... a few things I noticed/wondered

  • if this panics, the calling program will exit instead of propagating the panic to the Encode() caller... I'd expect the panic to propagate instead
  • is it better to start the json encoder async and the yaml decoder inline or vice-versa?
  • is it better to leave the pipereader unbuffered or wrap in a buffer?

Copy link
Author

Choose a reason for hiding this comment

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

FWIW, this is adapted from the official io.Pipe docs.

pipeWriter.Close()
}()
yamlErr := yamlDec.Decode(&intermediate)
if jsonErr != nil {
Copy link

Choose a reason for hiding this comment

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

I'm surprised this doesn't complain about races the same way

var intermediate interface{}
jsonEnc := json.NewEncoder(pipeWriter)
// We are using yaml.Decoder here (instead of json.Decoder) because the Go
// JSON library doesn't try to pick the right number type (int, float,
Copy link

Choose a reason for hiding this comment

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

also, a unit test demonstrating some of the edge cases like this would be helpful in ensuring this isn't producing user-visible changes

other examples of weird edges in yaml encoding/decoding we don't want to accidentally modify behavior on (even if the current behavior is not ultimately desired/correct):

// etc.) when unmarshalling to interface{}; it always picks float64.
// go-yaml preserves the number type.
yamlDec := yaml.NewDecoder(pipeReader)
yamlEnc := yaml.NewEncoder(e.w)
Copy link

Choose a reason for hiding this comment

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

This is doing an encode/decode/encode? Why? There has to be a better way than this? There should be some comment explaining the purpose?

@VictorLowther
Copy link

VictorLowther commented Sep 7, 2021 via email

@luxas
Copy link

luxas commented Sep 23, 2021

Cross-referencing #61 (comment) with information about potential blockers / info to think about with regards to this PR.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 3, 2021
@k8s-ci-robot
Copy link

@kevin-hanselman: PR needs rebase.

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/test-infra repository.

@ncdc
Copy link

ncdc commented Nov 3, 2021

/uncc

@k8s-ci-robot k8s-ci-robot removed the request for review from ncdc November 3, 2021 13:08
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 1, 2022
@kevin-hanselman
Copy link
Author

I currently do not have the bandwidth to push this PR forward. I'll close this PR until further notice, and I welcome someone else becoming its sponsor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. 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.

None yet

10 participants