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

(re)enable map key override for plain YAML #550

Merged
merged 2 commits into from
Dec 9, 2021

Conversation

pivotaljohn
Copy link
Contributor

@pivotaljohn pivotaljohn commented Nov 22, 2021

Addresses #310

Comment on lines 18 to 23
keyToIdxs := r.collectIndexesByKey(mapNode)
idxsToRemove := r.selectRedundantIdxs(keyToIdxs)
for _, idx := range idxsToRemove {
mapNode.Items = append(mapNode.Items[:idx], mapNode.Items[idx+1:]...)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

how about something like this (havent actually ran):

lastItemIndex := map[interface{}]int{}
for idx, item := range mapNode.Items {
	lastItemIndex[item.Key] = idx
}

newItems := []*MapItem{}
seenIndex := map[interface{}]struct{}{}
for _, item := range mapNode.Items {
	idx, found := lastItemIndex[item.Key]
	if !found {
		panic("Internal inconsistency: ...")
	}
	if _, found := seenIndex[item.Key]; !found {
		seenIndex[item.Key] = struct{}{}
		newItems = append(newItems, mapNode.Items[idx])
	}
}

mapNode.Items = newItems

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nice. 👍🏻 Far far simpler, no sorting, etc. And summarizing it sounds a lot like what the desired behavior is: keep only the last.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You'll notice I attempted something even more conceptually simpler.

Copy link
Contributor

Choose a reason for hiding this comment

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

And summarizing it sounds a lot like what the desired behavior is: keep only the last.

well keep only the last in the "space" of the first one -- because its effectively replacing it (vs delete+insert)

// Visitor performs an operation on the given Node while traversing the AST.
// Typically defines the action taken during a Walk().
type Visitor interface {
visit(Node) error
Copy link
Contributor

Choose a reason for hiding this comment

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

is this private intentionally? if so, any reason why not make walk private as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I did this intentionally. I didn't necessarily want to force the consuming programmer to export this function.

But one nice feature of an exported function name is that it's obviously the starting point in a set of functions attached to struct. So, I might just reverse this.

I see no causal connection between whether yamlmeta.Walk() should be exported and Visitor.visit be... In fact, in order for this walker to be useful in other packages, it would need to be exported.

Copy link
Contributor

Choose a reason for hiding this comment

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

In fact, in order for this walker to be useful in other packages, it would need to be exported.

yeah, that's what i was nudging towards -- nobody outside of this pkg would be able to impl Visitor if visit stays private.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 this, I stole the walk.go file for the story I'm working on, and struggled to use it without making it Public.

- introduce tree walker/visitor for AST.
@pivotaljohn pivotaljohn force-pushed the map-key-override-in-plain-yml branch from e5934eb to affd732 Compare December 3, 2021 15:53
expectedDocSet := docSet.DeepCopyAsNode()
docSet.OverrideMapKeys()

printer := yamlmeta.NewPrinterWithOpts(os.Stdout, yamlmeta.PrinterOpts{ExcludeRefs: true})
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: it's a little weird to have os.Stdout. lets throw in nil instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy-pasta from parser_test.go. I'll fix more broadly in a separate commit. 👍🏻

@pivotaljohn
Copy link
Contributor Author

@pivotaljohn pivotaljohn merged commit fdbc6a0 into develop Dec 9, 2021
@pivotaljohn pivotaljohn deleted the map-key-override-in-plain-yml branch December 9, 2021 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants