Reputation: 2221
I am having an issue with a cyclic dependency (import cycle not allowed) in my go code and not really sure the best way to resolve it. I think my lack of understanding of golang interfaces might be impacting my ability to see the way forward.
My Issue - I have Two Packages:
What is the best way to allow Workout to call a function (not a method on an object) in the Event domain?
Below is my simplified code to help provide some context
//workout package
//This is a private function on the workout package that is
//attempting to call a public function on the Event Package
func findWorkoutAssociatedToActivityTcx(txcObject *DataTcx) *EventWorkout{
return event.GetEventByDate(txcObject.ActivityDate, "", "")
}
Is it possible to create an interface into that function? I don't fully understand how I would do that. Thank you very much.
//UPDATES - Code on attempting to setup an interface
//WorkoutPackage
//workout package
//This is a private function on the workout package that is
//attempting to call a public function on the Event Package
func findWorkoutAssociatedToActivityTcx(txcObject *DataTcx, userID, transactionID string) *Workout {
//return event.GetEventByDate(txcObject.ActivityDate, "", "")
MyEventFinder.GetEventByDate(txcObject.ActivityDate, userID, transactionID)
return nil
}
var MyEventFinder EventFinder
type EventFinder interface {
GetEventByDate(time.Time, string, string) (*sharedstructs.ListOfEvents, error)
}
//Event Package
type eventProvider struct{}
func (e eventProvider) GetEventByDate(date time.Time, userID, transactionID string) (*sharedstructs.ListOfEvents, error) {
redFalconLogger.LogDebug("event.GetEventByDate: ", transactionID)
if date.IsZero() || userID == "" {
return nil, sharedstructs.InvalidData{Msg: "Invalid date or userID"}
}
//Create the query params
queryParamArray, queryParamCreationError := createQueryParamForQueryByDate(&date, &userID)
if queryParamCreationError != nil {
return nil, queryParamCreationError
}
//perform the query - pass empty orderBy because I don't care
queryResults, queryError := queryForEvent(*queryParamArray, "")
if queryError != nil {
switch queryError.(type) {
case firestorehelper.UnqueryableCollection:
return nil, sharedstructs.Forbidden{Msg: "operation is forbidden, probably due to malformed query"}
default:
return nil, sharedstructs.InternalServerError{Msg: "something went wrong in the query"}
}
}
return queryResults, nil
}
Upvotes: 0
Views: 417
Reputation: 3394
You are spot on that you can do this with an interface. In the event package you need to give the GetEventByDate a receiver struct:
type eventProvider struct {}
func (e eventProvider) GetEventByDate(t time.Time, a, b string) *workout.EventWorkout{...}
Then in the workout package:
type EventFinder interface {
GetEventByDate(time.Time, string, string) *EventWorkout
}
You can then pass an eventProvider
instance into the workout package and use that just via the EventFinder
interface that has no compile time dependency on the event
package. You don't show how you call into the workout
package but this could either be as a parameter to a method call or setting it when constructing a struct in the workout package.
Upvotes: 1