Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions cmd/avrogo/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ const nullType = "avrotypegen.Null"
// schema.RecordDefinition by looking at their match within given parsed namespace
func shouldImportAvroTypeGen(namespace *parser.Namespace, definitions []schema.QualifiedName) bool {
Copy link
Contributor

@rogpeppe rogpeppe Sep 2, 2024

Choose a reason for hiding this comment

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

I haven't looked in details but that == comparison is definitely wrong!

How about something like this instead, which keeps the binary search but (hopefully!) fixes the logic:

// shouldImportAvroTypeGen reports whether avrotypegen is required. It checks that the definitions given are of type
// schema.RecordDefinition by looking at their match within given parsed namespace
func shouldImportAvroTypeGen(namespace *parser.Namespace, definitions []schema.QualifiedName) bool {
	for _, def := range namespace.Definitions {
		searchName := def.AvroName().Name
		_, found := sort.Find(len(definitions), func(i int) int {
			return strings.Compare(searchName, definitions[i].Name)
		})
		if found {
			switch def.(type) {
			case *schema.RecordDefinition, *schema.FixedDefinition:
				return true
			}
		}
	}
	return false
}

Note: I haven't actually run the above code :)

Copy link
Author

Choose a reason for hiding this comment

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

It worked perfectly! I'll adjust the PR.

Thanks for the help, I really don't have much experience with the sort package and I was able to learn some cool stuff! 🙇‍♂️

for _, def := range namespace.Definitions {
defToGenerateIdx := sort.Search(len(definitions), func(i int) bool {
return definitions[i].Name == def.AvroName().Name
searchName := def.AvroName().Name
_, found := sort.Find(len(definitions), func(i int) int {
return strings.Compare(searchName, definitions[i].Name)
})
if defToGenerateIdx < len(definitions) && def.AvroName().Name == definitions[defToGenerateIdx].Name {
if _, ok := def.(*schema.RecordDefinition); ok {
return true
}
if _, ok := def.(*schema.FixedDefinition); ok {
if found {
switch def.(type) {
case *schema.RecordDefinition, *schema.FixedDefinition:
return true
}
}
Expand Down