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

Slowly migrate to range over func #313

Closed
petkostas opened this issue Jul 31, 2024 · 7 comments
Closed

Slowly migrate to range over func #313

petkostas opened this issue Jul 31, 2024 · 7 comments
Labels
enhancement New feature or request

Comments

@petkostas
Copy link

I have been struggling to find a good and clean solution for the introduced ordered map structs. Maybe consider and support the range-over-func experiment? It would remove the burden of having to re-create iterators for iterating over elements that are not natively supported by GO libraries 😄
I know it is still experimental, but I hardly find any reasons that this will not become stable in the long term.

@daveshanley
Copy link
Member

The ordered maps have a custom API, it's not ideal, but it means that we can swap out the underlying implementation underneath.

I don't think this is bad at all personally:

for schemaPairs := model.Model.Components.Schemas.First(); schemaPairs != nil; schemaPairs = schemaPairs.Next() {
  // get the name of the schema
  schemaName := schemaPairs.Key()

  // get the schema object from the map
  schemaValue := schemaPairs.Value()

  // build the schema
  schema := schemaValue.Schema()

  // if the schema has properties, print the number of properties
  if schema != nil && schema.Properties != nil {
    fmt.Printf("Schema '%s' has %d properties\n", schemaName, schema.Properties.Len())
  }
}

However, if someone is open to trying to implement this, I think it would be great.

@daveshanley daveshanley added the enhancement New feature or request label Jul 31, 2024
@petkostas
Copy link
Author

petkostas commented Jul 31, 2024

It is not bad! And the implementation is spot on! That is why I love your tools; I was considering that maybe it is a good alternative for the future that better integrates with libraries (e.g., templates).
Thanks for always being responsive @daveshanley

@petkostas
Copy link
Author

So, I gave it a quick spin today! It works like a charm.

func GetPaths(model *v3.Document) func(yield func(string, *v3.PathItem) bool) {
	iter := func(yield func(string, *v3.PathItem) bool) {
		for path := model.Paths.PathItems.First(); path != nil; path = path.Next() {
			if oas.IsExcluded(path.Value().Extensions) {
				continue
			}
			if !yield(path.Key(), path.Value()) {
				return
			}
		}
	}
	return iter
}

@daveshanley
Copy link
Member

So, I gave it a quick spin today! It works like a charm.

func GetPaths(model *v3.Document) func(yield func(string, *v3.PathItem) bool) {
	iter := func(yield func(string, *v3.PathItem) bool) {
		for path := model.Paths.PathItems.First(); path != nil; path = path.Next() {
			if oas.IsExcluded(path.Value().Extensions) {
				continue
			}
			if !yield(path.Key(), path.Value()) {
				return
			}
		}
	}
	return iter
}

Nice! this looks great.

What is this though? oas.IsExcluded ?

@petkostas
Copy link
Author

petkostas commented Aug 4, 2024

Ah I iterate over the elements and I check if they need to be excluded for my purpose (e.g. x-internal)
You can ignore that, as I modified this method for the example and forgot to remove that part.

@daveshanley
Copy link
Member

This has been addressed in #319

Courtesy of @TristanSpeakEasy

@daveshanley
Copy link
Member

Available in v0.17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants