-
Notifications
You must be signed in to change notification settings - Fork 260
Add WalkMetas* functions and meta.Name field #1089
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
Merged
openshift-merge-robot
merged 2 commits into
operator-framework:master
from
joelanford:walk-metas
May 5, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| package declcfg | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "go4.org/bytereplacer" | ||
|
|
||
| "github.com/operator-framework/operator-registry/alpha/property" | ||
| ) | ||
|
|
@@ -84,6 +90,7 @@ type RelatedImage struct { | |
| type Meta struct { | ||
| Schema string | ||
| Package string | ||
| Name string | ||
|
|
||
| Blob json.RawMessage | ||
| } | ||
|
|
@@ -93,17 +100,68 @@ func (m Meta) MarshalJSON() ([]byte, error) { | |
| } | ||
|
|
||
| func (m *Meta) UnmarshalJSON(blob []byte) error { | ||
| blob = bytereplacer.New(`\u003c`, "<", `\u003e`, ">", `\u0026`, "&").Replace(blob) | ||
|
|
||
| type tmp struct { | ||
|
stevekuznetsov marked this conversation as resolved.
|
||
| Schema string `json:"schema"` | ||
| Package string `json:"package,omitempty"` | ||
| Name string `json:"name,omitempty"` | ||
| Properties []property.Property `json:"properties,omitempty"` | ||
| } | ||
| var t tmp | ||
| if err := json.Unmarshal(blob, &t); err != nil { | ||
| return err | ||
| // TODO: return an error that includes the the full JSON message, | ||
| // the offset of the error, and the error message. Let callers | ||
| // decide how to format it. | ||
| return errors.New(resolveUnmarshalErr(blob, err)) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved this here because it was the only place access to the full json message existed after I removed the extra unmarshal to json.RawMessage. |
||
| } | ||
| m.Schema = t.Schema | ||
| m.Package = t.Package | ||
| m.Name = t.Name | ||
| m.Blob = blob | ||
| return nil | ||
| } | ||
|
|
||
| func resolveUnmarshalErr(data []byte, err error) string { | ||
| var te *json.UnmarshalTypeError | ||
| if errors.As(err, &te) { | ||
| return formatUnmarshallErrorString(data, te.Error(), te.Offset) | ||
| } | ||
| var se *json.SyntaxError | ||
| if errors.As(err, &se) { | ||
| return formatUnmarshallErrorString(data, se.Error(), se.Offset) | ||
| } | ||
| return err.Error() | ||
| } | ||
|
|
||
| func formatUnmarshallErrorString(data []byte, errmsg string, offset int64) string { | ||
| sb := new(strings.Builder) | ||
| _, _ = sb.WriteString(fmt.Sprintf("%s at offset %d (indicated by <==)\n ", errmsg, offset)) | ||
| // attempt to present the erroneous JSON in indented, human-readable format | ||
| // errors result in presenting the original, unformatted output | ||
| var pretty bytes.Buffer | ||
| err := json.Indent(&pretty, data, "", " ") | ||
| if err == nil { | ||
| pString := pretty.String() | ||
| // calc the prettified string offset which correlates to the original string offset | ||
| var pOffset, origOffset int64 | ||
| origOffset = 0 | ||
| for origOffset = 0; origOffset < offset; { | ||
| if pString[pOffset] != '\n' && pString[pOffset] != ' ' { | ||
| origOffset++ | ||
| } | ||
| pOffset++ | ||
| } | ||
| _, _ = sb.WriteString(pString[:pOffset]) | ||
| _, _ = sb.WriteString(" <== ") | ||
| _, _ = sb.WriteString(pString[pOffset:]) | ||
| } else { | ||
| for i := int64(0); i < offset; i++ { | ||
| _ = sb.WriteByte(data[i]) | ||
| } | ||
| _, _ = sb.WriteString(" <== ") | ||
| _, _ = sb.Write(data[offset:]) | ||
| } | ||
|
|
||
| return sb.String() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved the bytes replacement here, which meant I could drop that extra unmarshal call into
doc json.RawMessagewhen we're deciding the reader. Between the allocation improvement of a direct bytes replacer and the removal of the other unmarshal, we shave off ~7% of the runtime ofopm renderwhen I run it against the operatorhub catalog.Not too shabby!
In a follow-up, I might take a look at an alternate mode of
action.Render(maybe a new function alongsideRun()?), which lets the caller use a walk function. That would save a bunch on memory and might shave off more time as well.