Skip to content

Add interfaces to allow passing data between functions

What does this MR do?

Introduces a new interface, Analyzer, for the argument of NewCommands (and commands Run, Search, ...) to enable passing data between steps of analysis without changing the API.

originally to support the events feature:

Implementation

command.Config is split into two parts, an Analyzer and an Analysis. The Analyzer represents immutable configuration data and a factory function to create an Analysis, which is mutable.

  • Analyzer methods should remain pure, e.g. they shouldn't change the Analyzer instance. The only method that isn't a "getter" is NewAnalysis.
  • Analysis methods, which subsume the callback functions, can modify their receiver. That's the point of this MR 😄 .

Technically, it comes down to pointer versus non-pointer methods. The Analyzer methods on command.Config are non-pointer methods, while the Analyzer methods are pointer methods. E.g. consider the following:

Click to expand
	analyzer := command.Config{}
	analysis1 := analyzer.NewAnalysis()
	analysis2 := analyzer.NewAnalysis()

	fmt.Printf("original : %p\n", &analyzer)
	fmt.Printf("analysis1: %p\n", analysis1.(*command.Config))
	fmt.Printf("analysis2: %p\n", analysis2.(*command.Config))

All 3 pointers are different, because non-pointer receivers are struct copies. That's what we want, each call to Config.NewAnalysis returns a new copy.

In the previous implementation, assignment to RulesetConfig was local to the Run function's copy cfg only. The new design makes that explicit by moving the ruleset operations onto a new Analysis that's scoped to the Run function.

What are the relevant issue numbers?

Update command package to allow passing data be... (gitlab-org/gitlab#538315 - closed) • Jason Leasure • 18.0

Does this MR meet the acceptance criteria?

Edited by Julian Thome

Merge request reports

Loading