Skip to content

Instantly share code, notes, and snippets.

View reetasingh's full-sized avatar
👩‍💻
PR in progress ¯\_(ツ)_/¯

Reeta Singh reetasingh

👩‍💻
PR in progress ¯\_(ツ)_/¯
View GitHub Profile
type DBType interface {
LocalDatabase | InMemoryDatabase
}
type DBPointer[T any] interface {
*T
PersonDB
}
func CreatePersonDB[T DBType, dbPointer DBPointer[T]](person Person) error {
func CreatePersonDB[T PersonDB](person Person) error {
db := new(T)
db.save(person)
return nil
}
func main() {
person := Person{
ID: 101,
func CreatePersonDB[T PersonDB](person Person) error {
db := new(T)
db.save(person)
return nil
}
func main() {
person := Person{
ID: 101,
func CreatePersonDB(persondbtype string, person Person) error {
var db PersonDB
if persondbtype == "local" {
db = new(LocalDatabase)
} else if persondbtype == "in-memory" {
db = new(InMemoryDatabase)
} else {
return fmt.Errorf("persondbtype not supported :%s", persondbtype)
}
db.save(person)
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
type Person struct {
ID int
Name string
}
@reetasingh
reetasingh / dbmocking_with_mock_library.go
Created May 4, 2023 03:25
mocking PersonDB using mock package
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
@reetasingh
reetasingh / mockTestDB_return_configurable.go
Last active May 4, 2023 03:19
save function throwing error in one case and not throwing error in another case
type mockTestDB1 struct {}
type mockTestDB2 struct {}
func (m *mockTestDB1) Save(person *Person) error {
// case 1
return nil
}
func (m *mockTestDB2) Save(person *Person) error {
// case 2
@reetasingh
reetasingh / person_service_unit_test.go
Last active May 2, 2023 07:04
Person service unit test
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
type mockTestDB struct {
}
@reetasingh
reetasingh / person_service_loose_coupling.go
Created May 2, 2023 07:00
loose coupling without interfaces example
package main
import "fmt"
type Person struct {
ID int
Name string
}
type PersonDB interface {
@reetasingh
reetasingh / person_service.go
Created May 2, 2023 06:17
strong coupling without interfaces example
package main
import "fmt"
type Person struct {
ID int
Name string
}
type PersonDB struct {