I have the following function:
func (r *Resource) Create(kind string, data io.ReadCloser) (err error) {
decoder := json.NewDecoder(data)
r.Kind = kind
switch kind {
case "user":
var user User
if err = decoder.Decode(&user); err != nil {
panic(err)
}
if err = user.Save(r.Context); err != nil {
panic(err)
}
r.Data = user
break
case "space":
var space Space
if err = decoder.Decode(&space); err != nil {
panic(err)
}
if err = space.Save(r.Context); err != nil {
panic(err)
}
r.Data = space
break
case "room":
var room Room
if err = decoder.Decode(&room); err != nil {
panic(err)
}
if err = room.Save(r.Context); err != nil {
panic(err)
}
r.Data = room
break
case "element":
var element Element
if err = decoder.Decode(&element); err != nil {
panic(err)
}
if err = element.Save(r.Context); err != nil {
panic(err)
}
r.Data = element
break
default:
break
}
return
}
As you can see, each case in the switch is identical except for the type of the struct that receives the JSON data.
I suspect that there's an answer in interfaces and type assertion.
EDIT:
I was able to break out the saving part into a separate method, but I still can't figure out a good way to decode the JSON object into the appropriate struct without the switch statement.
func (r *Resource) Create(kind string, data io.ReadCloser) (err error) {
decoder := json.NewDecoder(data)
r.Kind = kind
switch kind {
case "user":
var user User
if err = decoder.Decode(&user); err != nil {
panic(err)
}
r.saveEntity(&user)
break
case "space":
var space Space
if err = decoder.Decode(&space); err != nil {
panic(err)
}
r.saveEntity(&space)
break
case "room":
var room Room
if err = decoder.Decode(&room); err != nil {
panic(err)
}
r.saveEntity(&room)
break
case "element":
var element Element
if err = decoder.Decode(&element); err != nil {
panic(err)
}
r.saveEntity(&element)
break
default:
break
}
return
}
func (r *Resource) saveEntity(e Entity) {
if err := e.Save(r.Context); err != nil {
panic(err)
}
r.Data = e
}
You could move the instantiation to one-line functions and create a mapping which maps the
kind to the respective instantiation function. The rest of the code should be re-usable.
Example:
kinds := map[string]func() Entity {
"user": func() Entity { return &User{} },
"space": func() Entity { return &Space{} },
"room": func() Entity { return &Room{} },
}
func Create(kind string) {
instance := kinds[kind]()
decoder.Decode(instance)
saveEntity(instance)
}
Related
I would like to make a user login from this method. This has to be in three parts. The same as user registration, but I am not understanding how to do this? Could you please write how I can write a user login logic this same way? It is created using gorilla mux.
One method has to be in db_service.go then one method has to be in login_service.go
one method has to be in login.go.
This is db_service.go code:
/* Used to create a singleton object of MongoDB client.
Initialized and exposed through GetMongoClient().*/
var clientInstance *mongo.Client
//Used during creation of singleton client object in GetMongoClient().
var clientInstanceError error
//Used to execute client creation procedure only once.
var mongoOnce sync.Once
//I have used below constants just to hold required database config's.
const (
CONNECTIONSTRING = "http://127.0.0.1:27017"
AUTH_DB = "Cluster0"
USER_COLLECTION = "user"
)
//GetMongoClient - Return mongodb connection to work with
func GetMongoClient() (*mongo.Client, error) {
//Perform connection creation operation only once.
mongoOnce.Do(func() {
// Set client options
clientOptions := options.Client().ApplyURI(CONNECTIONSTRING)
// Connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
clientInstanceError = err
}
// Check the connection
err = client.Ping(context.TODO(), nil)
if err != nil {
clientInstanceError = err
}
log.Println("Connected Mongodb!")
clientInstance = client
})
return clientInstance, clientInstanceError
}
//CreateIssue - Insert a new document in the collection.
func User_Collection(user *model.User) (*mongo.InsertOneResult, error) {
//Create a handle to the respective collection in the database.
collection := clientInstance.Database(AUTH_DB).Collection(USER_COLLECTION)
//Perform InsertOne operation & validate against the error.
return collection.InsertOne(context.TODO(), user)
}
login_service.go contained code for user registration:
func Register_User(user *model.User) (interface{}, error) {
user.CreatedAt = time.Now().UTC()
user.UpdatedAt = time.Now().UTC()
if result, err := util.User_Collection(user); err == nil {
return result.InsertedID, err
} else {
return nil, err
}
}
User registration code:
func Register(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var user model.User
if err := json.NewDecoder(r.Body).Decode(&user); err == nil {
if _, err := service.Register_User(&user); err == nil {
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(user)
} else {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(err)
}
} else {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
Something like this for the handler
func Login(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var user model.User
if err := json.NewDecoder(r.Body).Decode(&user); err == nil {
if _, err := service.Login_User(&user); err == nil {
json.NewEncoder(w).Encode(user)
} else {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(err)
}
} else {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
And something like this for the DB (Need to implement the query instead of insert)
func Login_User(user *model.User) (interface{}, error) {
// Implement this query
if result, err := util.Find_User(user); err == nil {
return result, err
} else {
return nil, err
}
}
When trying to run transaction on remove (Go):
func (dal *DAL) GetAndRemoveOneCode() (string, error) {
var session mongo.Session
var err error
var ctx = context.Background()
var result *mongo.DeleteResult
var codeContainer models.CodeContainer
var cursor *mongo.Cursor
for result == nil || result.DeletedCount != 1 {
var batchSize int32 = 1
pipeline := mongo.Pipeline{bson.D{{Key: "$sample", Value: bson.D{{Key: "size", Value: 1}}}}}
cursor, err = dal.mongodb.GetDatabase().Collection(collectionName).Aggregate(ctx, pipeline, &options.AggregateOptions{BatchSize: &batchSize})
if err != nil {
return "", err
}
for cursor.Next(ctx) {
// decode the document
if err := cursor.Decode(&codeContainer); err != nil {
return "", err
}
}
if session, err = dal.mongodb.GetDatabase().Client().StartSession(); err != nil {
return "", err
}
if err = session.StartTransaction(); err != nil {
return "", err
}
if err = mongo.WithSession(ctx, session, func(sc mongo.SessionContext) error {
if result, err = dal.mongodb.GetDatabase().Collection(collectionName).DeleteOne(sc, bson.D{{Key: "_id", Value: codeContainer.ID}}); err != nil {
return err
}
if result.DeletedCount != 1 {
return err
}
if err = session.CommitTransaction(sc); err != nil {
return err
}
return nil
}); err != nil {
return "", err
}
session.EndSession(ctx)
}
return codeContainer.Code, nil
}
I am getting the following (only in local, even after updating (version verified via mongod --version)):
ERROR:
Code: Unknown
Message: (IllegalOperation) Transaction numbers are only allowed on a replica set member or mongos
It works on Atlas, so it's only a local related issue
So my question is:
Is it possible to run transaction mon local mongod v4.2.8
Thanks
I create an api using golang, i would like to create some functionnal test, for that i create an interface to abstract my database. But for that i need to be able to convert the cursor to an array without knowing the type.
func (self *KeyController) GetKey(c echo.Context) (err error) {
var res []dto.Key
err = db.Keys.Find(bson.M{}, 10, 0, &res)
if err != nil {
fmt.Println(err)
return c.String(http.StatusInternalServerError, "internal error")
}
c.JSON(http.StatusOK, res)
return
}
//THE FIND FUNCTION ON THE DB PACKAGE
func (s MongoCollection) Find(filter bson.M, limit int, offset int, res interface{}) (err error) {
ctx := context.Background()
var cursor *mongo.Cursor
l := int64(limit)
o := int64(offset)
objectType := reflect.TypeOf(res).Elem()
cursor, err = s.c.Find(ctx, filter, &options.FindOptions{
Limit: &l,
Skip: &o,
})
if err != nil {
return
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
result := reflect.New(objectType).Interface()
err := cursor.Decode(&result)
if err != nil {
panic(err)
}
res = append(res.([]interface{}), result)
}
return
}
Does someone have an idea?
You can call directly the "All" method:
ctx := context.Background()
err = cursor.All(ctx, res)
if err != nil {
fmt.Println(err.Error())
}
For reference:
https://godoc.org/go.mongodb.org/mongo-driver/mongo#Cursor.All
i think you want to encapsulate the Find method for mongo query.
Using the reflect package i have improved your code by adding an additional parameter that serves as a template to instantiate new instances of slice items.
func (m *MongoDbModel) FindAll(database string, colname string, obj interface{}, parameter map[string]interface{}) ([]interface{}, error) {
var list = make([]interface{}, 0)
collection, err := m.Client.Database(database).Collection(colname).Clone()
objectType := reflect.TypeOf(obj).Elem()
fmt.Println("objectype", objectType)
if err != nil {
log.Println(err)
return nil, err
}
filter := bson.M{}
filter["$and"] = []bson.M{}
for key, value := range parameter {
filter["$and"] = append(filter["$and"].([]bson.M), bson.M{key: value})
}
cur, err := collection.Find(context.Background(), filter)
if err != nil {
log.Fatal(err)
}
defer cur.Close(context.Background())
for cur.Next(context.Background()) {
result := reflect.New(objectType).Interface()
err := cur.Decode(result)
if err != nil {
log.Println(err)
return nil, err
}
list = append(list, result)
}
if err := cur.Err(); err != nil {
return nil, err
}
return list, nil
}
The difference is that FindAll method returns []interface{}, where err := cur.Decode(result) directly consumes a pointer like the result variable.
I am using golang net/smtp to send mails
Whenever I send to my smtp server I need to capture the response from the server
Especially the DSN
For example my local smtp server gives a "ok queued as " at the end of the mail
I need to capture this and print in the logs
How can I do this
package main
import (
"log"
"net/smtp"
)
func sendEmail(msg []byte) {
c, err := smtp.Dial("localhost:25")
if err != nil {
log.Fatal(err)
}
if err := c.Mail("sender#example.org"); err != nil {
log.Fatal(err)
}
if err := c.Rcpt("recipient#example.net"); err != nil {
log.Fatal(err)
}
wc, err := c.Data()
if err != nil {
log.Fatal(err)
}
_, err = wc.Write(msg)
if err != nil {
log.Fatal(err)
}
//How do I get the response here ??
err = wc.Close()
if err != nil {
log.Fatal(err)
}
err = c.Quit()
if err != nil {
log.Fatal(err)
}
}
As mentioned in the comments you can use c.Text.ReadResponse():
package main
import (
"net/smtp"
)
func sendEmail(msg []byte) (code int, message string, err error) {
c, err := smtp.Dial("localhost:25")
if err != nil {
return
}
defer c.Quit() // make sure to quit the Client
if err = c.Mail("sender#example.org"); err != nil {
return
}
if err = c.Rcpt("recipient#example.net"); err != nil {
return
}
wc, err := c.Data()
if err != nil {
return
}
defer wc.Close() // make sure WriterCloser gets closed
_, err = wc.Write(msg)
if err != nil {
return
}
code, message, err = c.Text.ReadResponse(0)
return
}
The code, message and any err are now passed to the caller, don't use log.Fatal throughout your code, handle the error on the calling side.
package main
import (
"net/smtp"
)
func sendEmail(msg []byte) (code int, message string, err error) {
c, err := smtp.Dial("localhost:25")
if err != nil {
return
}
defer c.Quit() // make sure to quit the Client
if err = c.Mail("sender#example.org"); err != nil {
return
}
if err = c.Rcpt("recipient#example.net"); err != nil {
return
}
wc, err := c.Data()
if err != nil {
return
}
_, err = wc.Write(msg)
if err != nil {
return
}
code, message, err = closeData(c)
if err != nil {
return 0, "", err
}
return code, message, err
}
func closeData(client *smtp.Client) error {
d := &dataCloser{
c: client,
WriteCloser: client.Text.DotWriter(),
}
return d.Close()
}
type dataCloser struct {
c *smtp.Client
io.WriteCloser
}
func (d *dataCloser) Close() (int, string, error) {
d.WriteCloser.Close() // make sure WriterCloser gets closed
code, message, err := d.c.Text.ReadResponse(250)
fmt.Printf("Message %v, Error %v\n", message, err)
return code, message, err
}
I'm trying to write a simple sockets based go server. I'm just wondering how does the connection.Read below knows when to stop reading.
(Note: this is not my code, I copied it from Unix Sockets in Go as example)
package main
import (
"log"
"net"
)
func echoServer(c net.Conn) {
for {
buf := make([]byte, 512)
nr, err := c.Read(buf)
if err != nil {
return
}
data := buf[0:nr]
println("Server got:", string(data))
_, err = c.Write(data)
if err != nil {
log.Fatal("Write: ", err)
}
}
}
func main() {
l, err := net.Listen("unix", "/tmp/echo.sock")
if err != nil {
log.Fatal("listen error:", err)
}
for {
fd, err := l.Accept()
if err != nil {
log.Fatal("accept error:", err)
}
go echoServer(fd)
}
}
Is it the EOF character or there's something else?
It would be really helpful if someone can point me to a link official go docs. Thanks.
This is the implementation of the default Read method on net.Conn.Read:
// Read implements the Conn Read method.
func (c *conn) Read(b []byte) (int, error) {
if !c.ok() {
return 0, syscall.EINVAL
}
n, err := c.fd.Read(b)
if err != nil && err != io.EOF {
err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
}
return n, err
}
This is the implementation of the c.fd.Read(b) that is called within the function above:
func (fd *netFD) Read(p []byte) (n int, err error) {
if err := fd.readLock(); err != nil {
return 0, err
}
defer fd.readUnlock()
if len(p) == 0 {
// If the caller wanted a zero byte read, return immediately
// without trying. (But after acquiring the readLock.) Otherwise
// syscall.Read returns 0, nil and eofError turns that into
// io.EOF.
// TODO(bradfitz): make it wait for readability? (Issue 15735)
return 0, nil
}
if err := fd.pd.prepareRead(); err != nil {
return 0, err
}
if fd.isStream && len(p) > 1<<30 {
p = p[:1<<30]
}
for {
n, err = syscall.Read(fd.sysfd, p)
if err != nil {
n = 0
if err == syscall.EAGAIN {
if err = fd.pd.waitRead(); err == nil {
continue
}
}
}
err = fd.eofError(n, err)
break
}
if _, ok := err.(syscall.Errno); ok {
err = os.NewSyscallError("read", err)
}
return
}
So, yes, an EOF will make it stop reading. But so will plenty of other non-nil errors.
It will stop reading when its underlying implementation hits any error.
Error may be an actual I/O error, or it could be the operating system signaling connection closed with io.EOF, or it could be a timeout, so on.