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

USP Builder #148

Open
derickdiaz opened this issue Feb 26, 2024 · 1 comment
Open

USP Builder #148

derickdiaz opened this issue Feb 26, 2024 · 1 comment

Comments

@derickdiaz
Copy link

To build an adapter within this project you must define struct that integrates all the functions within the USPAdapter interface, currently this would only be close() error. Additionally, the adapter must be constructed within the runAdapter function in the tools.go file. The runAdapter function has a big if/else if/else statement that can grow in size and become problematic as more adapters are build. I would like to propose an ideas to help keep this function small and define what it means to build an adapter:

  1. Create an AdapterBuilder interface

The AdapterBuilder of any struct has the purpose of building the adapter. For example, the AdapterBuilder struct will take any information required to build the adapter, i.e the clientConfigs. Below is an example of the interface

type USPAdapterBuilder interface {
	NewAdapter() (USPAdapter, error)
	GetMethodName() string
	GetClientOptions() *ClientOptions
}

The GetMethodName will return a string correlating to the string comparison within the current configuration of the runAdapter function. This will allow the runAdapter method to be shorten like this:

func buildAdapter(method string, healthChecks bool) (USPAdapter, error) {
	builders := []USPAdapterBuilder{
		&MicrosoftUSPAdapterBuilder{},
	}

	builder, err := getBuilderByMethodName(method, builders)
	if err != nil {
		return nil, err
	}

	configureBuilderClientOptions(builder.GetClientOptions())
	adapter, err := builder.NewAdapter()
	if err != nil {
		return nil, err
	}

	if healthChecks {
		performHealthChecksOnAdapter(adapter)
	}

	return adapter, nil
}

func getBuilderByMethodName(method string, builders []USPAdapterBuilder) (USPAdapterBuilder, error) {
	for _, builder := range builders {
		if builder.GetMethodName() == method {
			return builder, nil
		}
	}
	return nil, fmt.Errorf("Unable to find builder for method name: %s", method)
}

User will define a builder based on the UserAdapterBuilder interface and to integrate It to the application, they would have to update the list. I'm sure there's a better way to handle by treating adapters as plugin that way the code does not have to re-compile every time a new adapter is added. Additionally, this solution would require all the adapters to change.

@derickdiaz
Copy link
Author

In this example builder is not the correct term. Factory is a better term since it matches that design pattern.

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

No branches or pull requests

1 participant