2016-04-02 4 views
0

folgende struct Gegeben erstellen:Golang & mgo: Wie eine generische Einheit mit gemeinsamen Bereichen wie _id, Erstellungszeit und letzte Aktualisierung

package models 

import (
    "time" 
    "gopkg.in/mgo.v2/bson" 
) 

type User struct { 
    Id   bson.ObjectId `json:"id" bson:"_id"` 
    Name  string  `json:"name" bson:"name"` 
    BirthDate time.Time  `json:"birth_date" bson:"birth_date"` 
    InsertedAt time.Time  `json:"inserted_at" bson:"inserted_at"` 
    LastUpdate time.Time  `json:"last_update" bson:"last_update"` 
} 

... hier ist, wie ich einen neuen Benutzer in eine einfügen Mongo Sammlung:

user := &models.User{ 
    bson.NewObjectId(), 
    "John Belushi", 
    time.Date(1949, 01, 24), 
    time.now().UTC(), 
    time.now().UTC(), 
} 

dao.session.DB("test").C("users").Insert(user) 

Ist es möglich, eine generische Entity alle anderen Einheiten von erben zu haben? Ich habe schon versucht, diese ...

type Entity struct { 
    Id   bson.ObjectId `json:"id" bson:"_id"` 
    InsertedAt time.Time  `json:"inserted_at" bson:"inserted_at"` 
    LastUpdate time.Time  `json:"last_update" bson:"last_update"` 
} 

type User struct { 
    Entity 
    Name  string  `json:"name" bson:"name"` 
    BirthDate time.Time  `json:"birth_date" bson:"birth_date"` 
} 

... aber dies bedeutet ein endgültiges Ergebnis wie folgt aus:

{ 
    "Entity": { 
     "_id": "...", 
     "inserted_at": "...", 
     "last_update": "..." 
    }, 
    "name": "John Belushi", 
    "birth_date": "1949-01-24..." 
} 

Wie kann ich das folgende Ergebnis erhalten, ohne in jedem struct gemeinsame Felder zu wiederholen?

{ 
    "_id": "...", 
    "inserted_at": "...", 
    "last_update": "...", 
    "name": "John Belushi", 
    "birth_date": "1949-01-24..." 
} 
+1

Haben Sie es tatsächlich versucht? http://play.golang.org/p/7OFHTQB0I_ – OneOfOne

Antwort

1

Dies wurde bereits in Storing nested structs with mgo beantwortet, aber es ist sehr einfach, alles, was Sie tun müssen, ist bson:",inline" auf der anonymen innere Struktur hinzufügen und als normal ...

Hier ein kurzes Beispiel initialisieren:

package main 

import (
    "gopkg.in/mgo.v2" 
    "gopkg.in/mgo.v2/bson" 
) 

type Entity struct { 
    Id   bson.ObjectId `json:"id" bson:"_id"` 
    InsertedAt time.Time  `json:"inserted_at" bson:"inserted_at"` 
    LastUpdate time.Time  `json:"last_update" bson:"last_update"` 
} 

type User struct { 
    Entity `bson:",inline"` 
    Name  string `json:"name" bson:"name"` 
    BirthDate time.Time `json:"birth_date" bson:"birth_date"` 
} 

func main() { 
    info := &mgo.DialInfo{ 
     Addrs: []string{"localhost:27017"}, 
     Timeout: 60 * time.Second, 
     Database: "test", 
    } 

    session, err := mgo.DialWithInfo(info) 
    if err != nil { 
     panic(err) 
    } 
    defer session.Close() 
    session.SetMode(mgo.Monotonic, true) 
    // c := session.DB("test").C("users") 

    user := User{ 
     Entity: Entity{"123456789098", time.Now().UTC(), time.Now().UTC()}, 
     Name:  "John Belushi", 
     BirthDate: time.Date(1959, time.February, 28, 0, 0, 0, 0, time.UTC), 
    } 

    session.DB("test").C("users").Insert(user) 
} 
Verwandte Themen