How to Implement Dynamic Properties in Google App Engine\'s Go Datastore?

Mary-Kate Olsen
Release: 2024-11-26 05:57:10
Original
682 people have browsed it

How to Implement Dynamic Properties in Google App Engine's Go Datastore?

Adding Dynamic Properties to Entities in Go

In the Google App Engine datastore, you may encounter scenarios where you need to define properties for entities dynamically. This article explores a technique similar to Python's Expando Model to achieve dynamic property handling in Go.

Property Load Saver Interface

The key to creating dynamic properties lies in the PropertyLoadSaver interface. By implementing this interface, you can dynamically construct the properties of an entity at save time.

PropertyList Type

Conveniently, the Go AppEngine platform provides the PropertyList type, which is a slice of Property objects and also implements PropertyLoadSaver. To create an Expando model in Go, simply use the PropertyList type. You can add the desired properties to it, and these will be saved as part of the entity.

package main

import (
    "context"
    "time"

    "google.golang.org/appengine/datastore"
)

func main() {
    ctx := context.Background()

    props := datastore.PropertyList{
        datastore.Property{Name: "time", Value: time.Now()},
        datastore.Property{Name: "email", Value: "example@email.com"},
    }

    k := datastore.NewIncompleteKey(ctx, "DynEntity", nil)
    key, err := datastore.Put(ctx, k, &props)
    if err != nil {
        // Handle error
    }

    c.Infof("%v", key)
}
Copy after login

In this example, an entity named "DynEntity" is saved with two dynamic properties: "time" and "email."

Native Map Implementation

You can also implement the PropertyLoadSaver interface with a custom type, such as a map. The following code snippet demonstrates how to create a DynEnt type that wraps a map:

type DynEnt map[string]interface{}

func (d *DynEnt) Load(props []datastore.Property) error {
    for _, p := range props {
        (*d)[p.Name] = p.Value
    }
    return nil
}

func (d *DynEnt) Save() (props []datastore.Property, err error) {
    for k, v := range *d {
        props = append(props, datastore.Property{Name: k, Value: v})
    }
    return
}
Copy after login

This custom type can be used to load and save entities dynamically:

d := DynEnt{"email": "example@email.com", "time": time.Now()}

k := datastore.NewIncompleteKey(ctx, "DynEntity", nil)
key, err := datastore.Put(ctx, k, &d)
if err != nil {
    // Handle error
}

c.Infof("%v", key)
Copy after login

This approach provides a flexible way to add dynamic properties to entities in the Google App Engine datastore.

The above is the detailed content of How to Implement Dynamic Properties in Google App Engine\'s Go Datastore?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template