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

Generics-based resolver #19

Merged
merged 5 commits into from
Nov 23, 2023
Merged

Generics-based resolver #19

merged 5 commits into from
Nov 23, 2023

Conversation

cmaglie
Copy link
Member

@cmaglie cmaglie commented Nov 22, 2023

This PR provides a Resolver object that can be used to resolve dependencies without the need for the caller to box/unbox his concrete structs into semver.Release and semver.Dependency interfaces.

The old way required something like this:

	type customRel struct {
		...
		Deps []semver.Dependency
	}
	a100 := &customRel{...., Deps: []*customDep{....} } // Boxing *customDep into sermver.Dependecy
	a110 := &customRel{...., Deps: []*customDep{....} }
	a120 := &customRel{...., Deps: []*customDep{....} }
	arch := &Archive{
		Releases: map[string]Releases{
			"A": {a100, a110, a111, a120, a121},   // Boxing *customRel into semver.Release
			"B": {b131, b130, b121, b120, b111, b110, b100},
			"C": {c200, c120, c111, c110, c102, c101, c100, c021, c020, c010},
			"D": {d100, d120},
			"E": {e100, e101},
			"G": {g130, g140, g150, g160, g170, g180},
			"H": {h130, h140, h150, h160, h170, h180},
			"I": {i130, i140, i150, i160, i170, i180},
		},
	}
	boxedRes := arch.Resolve(...)
	res := []*customRel{}
	for _, r := range res {
		res = append(res, *customRel(r))  // Unbox semver.Release into *customRel
	}

now we can avoid type conversion using the generic constructor:

	a100 := &customRel{.... &customDep{....} }
	a110 := &customRel{.... &customDep{....} }
	a120 := &customRel{.... &customDep{....} }
	// ...
	arch := NewResolver[*customRel, *customDep]() // Specify custom types using generics declaration
	arch.AddReleases(   // No need to box in semver.Release interface
		a100, a110, a111, a120, a121,
		b131, b130, b121, b120, b111, b110, b100,
		c200, c120, c111, c110, c102, c101, c100, c021, c020, c010,
		d100, d120,
		e100, e101,
		g130, g140, g150, g160, g170, g180,
		h130, h140, h150, h160, h170, h180,
		i130, i140, i150, i160, i170, i180,
	)

	res := arch.Resolve(....) // res is already a []*customRel, no need to unbox

@cmaglie cmaglie self-assigned this Nov 22, 2023
@cmaglie cmaglie added the enhancement New feature or request label Nov 22, 2023
Copy link

codecov bot commented Nov 22, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (e8b9735) 100.00% compared to head (ae9c7ba) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #19   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           11        11           
  Lines          858       876   +18     
=========================================
+ Hits           858       876   +18     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cmaglie cmaglie merged commit 391e859 into master Nov 23, 2023
5 checks passed
@cmaglie cmaglie deleted the templated_resolver branch November 23, 2023 13:41
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

Successfully merging this pull request may close these issues.

2 participants