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

Fix 1455 and retain invalid frontmatter over failing/overwriting #627

Merged
merged 4 commits into from
Jul 6, 2024

Conversation

sourishkrout
Copy link
Member

@sourishkrout sourishkrout commented Jul 5, 2024

Turns out people use templating languages and interpolation in frontmatter which makes it invalid unless rendered first. The idea of this PR is to pass-thru frontmatter when attempts to parsing it fail.

Fixes stateful/vscode-runme#1455

@sourishkrout sourishkrout marked this pull request as draft July 5, 2024 22:25
@sourishkrout sourishkrout requested a review from adambabik July 5, 2024 23:57
@sourishkrout sourishkrout marked this pull request as ready for review July 5, 2024 23:57
@sourishkrout
Copy link
Member Author

Lemme know what you think @adambabik. Making checking for errors optional seemed like the best way forward to solve for the PR's description/linked issue.

Copy link
Collaborator

@adambabik adambabik left a comment

Choose a reason for hiding this comment

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

I think the solution makes sense. I added a few comments on how the public APIs can be improved.

@@ -17,17 +18,19 @@ const (
DocumentID = "id"
)

func Deserialize(data []byte, identityResolver *identity.IdentityResolver) (*Notebook, error) {
func Deserialize(log *zap.Logger, data []byte, identityResolver *identity.IdentityResolver) (*Notebook, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggestion: as this interface will likely get extended, I suggest this:

Suggested change
func Deserialize(log *zap.Logger, data []byte, identityResolver *identity.IdentityResolver) (*Notebook, error) {
type Options struct {
IdentityResolver *identity.IdentityResolver
Logger *zap.Logger
}
func Deserialize(data []byte, opts Options) (*Notebook, error) {

@@ -116,6 +116,10 @@ func (d *Document) splitAndParse() error {
return nil
}

func (d *Document) FrontMatterRaw() []byte {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggestion: Let's stick with Frontmatter for consistency.

Suggested change
func (d *Document) FrontMatterRaw() []byte {
func (d *Document) FrontmatterRaw() []byte {

return d.frontmatter
}

func (d *Document) FrontmatterError() error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggestion: We can have two methods: Frontmatter() *Frontmatter and FrontmatterWithError() (*Frontmatter, error). I think it will be a bit more clear and the caller will only need to use one method when it wants to handle frontmatter's error.

Suggested change
func (d *Document) FrontmatterError() error {
func (d *Document) FrontmatterWithError() (*Frontmatter, error) {

@@ -30,7 +33,7 @@ func Format(files []string, basePath string, flatten bool, formatJSON bool, writ
identityResolver := identity.NewResolver(identity.DefaultLifecycleIdentity)

if flatten {
notebook, err := editor.Deserialize(data, identityResolver)
notebook, err := editor.Deserialize(logger, data, identityResolver)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remark: With my earlier suggestion, this will become:

Suggested change
notebook, err := editor.Deserialize(logger, data, identityResolver)
notebook, err := editor.Deserialize(data, editor.Options{IdentityResolver: identityResolver})

The logger can be figure out by editor.Deserialize and default to noop.

@sourishkrout
Copy link
Member Author

@adambabik feedback makes perfect sense 👌. Thank you.

@sourishkrout sourishkrout force-pushed the fix-1455-invalid-frontmatter branch from 6a62664 to 45a1f4b Compare July 6, 2024 16:55
Copy link

sonarcloud bot commented Jul 6, 2024

@sourishkrout sourishkrout merged commit 4bcb527 into main Jul 6, 2024
7 checks passed
@sourishkrout sourishkrout deleted the fix-1455-invalid-frontmatter branch July 6, 2024 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ignore non-recognized markdown contents
2 participants