azure-service-operator icon indicating copy to clipboard operation
azure-service-operator copied to clipboard

Improve: filter Swagger input files before loading them, to improve speed of generator

Open Porges opened this issue 4 years ago • 1 comments

I think with functions like:

func (t *TypeMatcher) AppliesToAnyTypesInPackage(p astmodel.LocalPackageReference) bool {
	return t.groupMatches(p.Group()) && t.versionMatches(p.Version())
}

func (t *TypeMatcher) AppliesToWholePackage(p astmodel.LocalPackageReference) bool {
	return t.groupMatches(p.Group()) && t.versionMatches(p.Version()) && t.Name == ""
}

We can write a function like:

func fileShouldBeIncluded(filters []*config.TypeFilter, exportFilters []*config.ExportFilter, localPackage astmodel.LocalPackageReference) (string, bool) {
	for _, filter := range filters {
		if filter.Action == config.TypeFilterInclude && filter.AppliesToAnyTypesInPackage(localPackage) {
			return filter.Because, true
		}

		if filter.Action == config.TypeFilterPrune && filter.AppliesToWholePackage(localPackage) {
			return filter.Because, false
		}
	}

	for _, exportFilter := range exportFilters {
		if exportFilter.Action == config.ExportFilterInclude && exportFilter.AppliesToAnyTypesInPackage(localPackage) {
			return exportFilter.Because, true
		}

		if exportFilter.Action == config.ExportFilterExclude && exportFilter.AppliesToWholePackage(localPackage) {
			return exportFilter.Because, false
		}
	}

	// default to true
	return "", true
}

Logic to be checked!

Driving code:

			group := jsonast.SwaggerGroupRegex.FindString(filePath)
			version := strings.Trim(swaggerVersionRegex.FindString(filePath), "/")
			localPackage := astmodel.MakeLocalPackageReference(
				packagePrefix,
				group,
				version,
			)

			because, include := fileShouldBeIncluded(filters, exportFilters, localPackage)
			if !include {
				klog.V(3).Infof("Skipping whole file %q, because: %s", filePath, because)
				return nil
			}

Porges avatar Sep 28 '21 20:09 Porges

We still see value in this.

theunrepentantgeek avatar Jul 04 '22 22:07 theunrepentantgeek

Still interested.

theunrepentantgeek avatar Dec 13 '22 02:12 theunrepentantgeek

Could we do something based purely on group, even if we can't be more fine-grained than that?

matthchr avatar May 22 '23 23:05 matthchr

We still want to do something to reduce the workload of the generator.

theunrepentantgeek avatar Aug 07 '23 23:08 theunrepentantgeek

We're still interested in this, although we have done some in this space to speed things up (@theunrepentantgeek thinks by group).

matthchr avatar Nov 27 '23 23:11 matthchr

Sufficiently addressed by #3803

theunrepentantgeek avatar Feb 23 '24 00:02 theunrepentantgeek