Code Monkey home page Code Monkey logo

serializable_meta's Introduction

Serializable Meta

Serializable Meta allows the developer to specify, for a given model, a custom serialization model along with field mappings. This mechanism thus allows one model to act as another model when it comes to serialization.

GoDoc

Usage

The example herein shows how to manage different kinds of background jobs using https://github.com/qor/serializable_meta.

Define serializable model

Define QorJob model and embed serializable_meta.SerializableMeta to apply the feature.

type QorJob struct {
  gorm.Model
  Name string
  serializable_meta.SerializableMeta
}

Add function GetSerializableArgumentResource to the model, so Serializable Meta can know the type of argument. Then define background jobs.

func (qorJob QorJob) GetSerializableArgumentResource() *admin.Resource {
  return jobsArgumentsMap[qorJob.Kind]
}

var jobsArgumentsMap = map[string]*admin.Resource{
  "newsletter": admin.NewResource(&sendNewsletterArgument{}),
  "import_products": admin.NewResource(&importProductArgument{}),
}

type sendNewsletterArgument struct {
  Subject string
  Content string
}

type importProductArgument struct {}

Use serializable features

At first, Set a job's Name, Kind and SetSerializableArgumentValue. Then save it into database.

var qorJob QorJob
qorJob.Name = "sending newsletter"
qorJob.Kind = "newsletter"
qorJob.SetSerializableArgumentValue(&sendNewsletterArgument{
  Subject: "subject",
  Content: "content",
})

db.Create(&qorJob)

This will marshal sendNewsletterArgument as a json, and save it into database by this SQL

INSERT INTO "qor_jobs" (kind, value) VALUES (`newsletter`, `{"Subject":"subject","Content":"content"}`);

Now you can fetch the saved QorJob from the database. And get the serialized data from the record.

var result QorJob
db.First(&result, "name = ?", "sending newsletter")

var argument = result.GetSerializableArgument(result)
argument.(*sendNewsletterArgument).Subject // "subject"
argument.(*sendNewsletterArgument).Content // "content"

License

Released under the MIT License.

serializable_meta's People

Contributors

binku87 avatar bom-d-van avatar jasonweng avatar jinzhu avatar raven-chen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.