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:
- Event - Event is mostly a "parent" and will make many calls into the workout package
- Workout - In one case I need to call the Event Package and this creates a cyclic dependency because Event already consumes Workout
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
}