本文整理匯總了Golang中github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/pq/hstore.Hstore.Map方法的典型用法代碼示例。如果您正苦於以下問題:Golang Hstore.Map方法的具體用法?Golang Hstore.Map怎麽用?Golang Hstore.Map使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/pq/hstore.Hstore
的用法示例。
在下文中一共展示了Hstore.Map方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Add
func (r *AppRepo) Add(data interface{}) error {
app := data.(*ct.App)
if app.Name == "" {
return ct.ValidationError{"name", "must not be blank"}
}
if len(app.Name) > 30 || !appNamePattern.MatchString(app.Name) {
return ct.ValidationError{"name", "is invalid"}
}
if app.ID == "" {
app.ID = utils.UUID()
}
var meta hstore.Hstore
if len(app.Meta) > 0 {
meta.Map = make(map[string]sql.NullString, len(app.Meta))
for k, v := range app.Meta {
meta.Map[k] = sql.NullString{String: v, Valid: true}
}
}
err := r.db.QueryRow("INSERT INTO apps (app_id, name, protected, meta) VALUES ($1, $2, $3, $4) RETURNING created_at, updated_at", app.ID, app.Name, app.Protected, meta).Scan(&app.CreatedAt, &app.UpdatedAt)
app.ID = cleanUUID(app.ID)
if !app.Protected && r.defaultDomain != "" {
route := (&strowger.HTTPRoute{
Domain: fmt.Sprintf("%s.%s", app.Name, r.defaultDomain),
Service: app.Name + "-web",
}).ToRoute()
route.ParentRef = routeParentRef(app)
if err := r.router.CreateRoute(route); err != nil {
log.Printf("Error creating default route for %s: %s", app.Name, err)
}
}
return err
}
示例2: Update
func (r *AppRepo) Update(id string, data map[string]interface{}) (interface{}, error) {
tx, err := r.db.Begin()
if err != nil {
return nil, err
}
app, err := selectApp(tx, id, true)
if err != nil {
tx.Rollback()
return nil, err
}
for k, v := range data {
switch k {
case "strategy":
strategy, ok := v.(string)
if !ok {
tx.Rollback()
return nil, fmt.Errorf("controller: expected string, got %T", v)
}
app.Strategy = strategy
if _, err := tx.Exec("UPDATE apps SET strategy = $2, updated_at = now() WHERE app_id = $1", app.ID, app.Strategy); err != nil {
tx.Rollback()
return nil, err
}
case "meta":
data, ok := v.(map[string]interface{})
if !ok {
tx.Rollback()
return nil, fmt.Errorf("controller: expected map[string]interface{}, got %T", v)
}
var meta hstore.Hstore
meta.Map = make(map[string]sql.NullString, len(data))
app.Meta = make(map[string]string, len(data))
for k, v := range data {
s, ok := v.(string)
if !ok {
tx.Rollback()
return nil, fmt.Errorf("controller: expected string, got %T", v)
}
meta.Map[k] = sql.NullString{String: s, Valid: true}
app.Meta[k] = s
}
if _, err := tx.Exec("UPDATE apps SET meta = $2, updated_at = now() WHERE app_id = $1", app.ID, meta); err != nil {
tx.Rollback()
return nil, err
}
}
}
if err := createEvent(tx.Exec, &ct.Event{
AppID: app.ID,
ObjectID: app.ID,
ObjectType: ct.EventTypeApp,
}, app); err != nil {
tx.Rollback()
return nil, err
}
return app, tx.Commit()
}
示例3: metaToHstore
func metaToHstore(m map[string]string) hstore.Hstore {
var s hstore.Hstore
if len(m) > 0 {
s.Map = make(map[string]sql.NullString, len(m))
for k, v := range m {
s.Map[k] = sql.NullString{String: v, Valid: true}
}
}
return s
}
示例4: Update
func (r *AppRepo) Update(id string, data map[string]interface{}) (interface{}, error) {
tx, err := r.db.Begin()
if err != nil {
return nil, err
}
app, err := selectApp(tx, id, true)
if err != nil {
tx.Rollback()
return nil, err
}
for k, v := range data {
switch k {
case "protected":
protected, ok := v.(bool)
if !ok {
tx.Rollback()
return nil, fmt.Errorf("controller: expected bool, got %T", v)
}
if app.Protected != protected {
if _, err := tx.Exec("UPDATE apps SET protected = $2, updated_at = now() WHERE app_id = $1", app.ID, protected); err != nil {
tx.Rollback()
return nil, err
}
app.Protected = protected
}
case "meta":
data, ok := v.(map[string]interface{})
if !ok {
tx.Rollback()
return nil, fmt.Errorf("controller: expected map[string]interface{}, got %T", v)
}
var meta hstore.Hstore
meta.Map = make(map[string]sql.NullString, len(data))
app.Meta = make(map[string]string, len(data))
for k, v := range data {
s, ok := v.(string)
if !ok {
tx.Rollback()
return nil, fmt.Errorf("controller: expected string, got %T", v)
}
meta.Map[k] = sql.NullString{String: s, Valid: true}
app.Meta[k] = s
}
if _, err := tx.Exec("UPDATE apps SET meta = $2, updated_at = now() WHERE app_id = $1", app.ID, meta); err != nil {
tx.Rollback()
return nil, err
}
}
}
return app, tx.Commit()
}