How to Implement Dynamic Properties in the Go App Engine Datastore?

Susan Sarandon
Release: 2024-11-21 07:43:10
Original
226 people have browsed it

How to Implement Dynamic Properties in the Go App Engine Datastore?

Dynamic Properties in the Go App Engine Datastore

Unlike Python's App Engine, Go does not offer an out-of-the-box mechanism for creating entities with dynamic properties, where attributes can be assigned at runtime. However, using the PropertyLoadSaver interface, it is possible to implement similar functionality.

Using PropertyList for Dynamic Properties

Go's App Engine platform provides the PropertyList type, which implements the PropertyLoadSaver interface and allows you to construct properties dynamically at save time. Consider the following example:

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@example.com"},
    }

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

    // ...
}
Copy after login

In this example, we create an entity named "DynEntity" with two dynamic properties, "time" and "email."

Creating a Custom Type for Dynamic Properties

In some cases, it may be preferable to implement the PropertyLoadSaver interface on a custom type rather than using PropertyList. For instance, you could implement it on a map type:

package main

import (
    "context"

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

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() ([]datastore.Property, error) {
    var props []datastore.Property
    for k, v := range *d {
        props = append(props, datastore.Property{Name: k, Value: v})
    }
    return props, nil
}

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

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

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

    // ...
}
Copy after login

With this custom type, you can use dynamic properties like any other map in Go, but they can be stored and retrieved from the datastore:

d["active"] = true
datastore.Put(ctx, key, &d)
Copy after login

The above is the detailed content of How to Implement Dynamic Properties in the Go App Engine 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