diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..665818c --- /dev/null +++ b/.drone.yml @@ -0,0 +1,28 @@ +--- +kind: pipeline +name: test +steps: + - name: test + image: golang:1.18rc1-alpine3.15 + commands: + - apk add build-base redis + - redis-server & + - go test + - name: notify + image: plugins/slack + settings: + username: Grocery CI + icon_emoji: ":shopping_trolley:" + channel: rd-bots + template: > + {{#success build.status}} + Build {{build.number}} for ${DRONE_BRANCH} on ${DRONE_REPO} succeeded! :party-gopher: + {{else}} + Build {{build.number}} for ${DRONE_BRANCH} on ${DRONE_REPO} failed! :doh: + {{/success}} + webhook: + from_secret: slack_webhook + when: + branch: main + status: [success, failure] + event: push diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..92486d8 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,6 @@ +# This is a comment. +# Each line is a file pattern followed by one or more owners. + +# These owners will be the default owners for everything in +# the repo. +* @jackcook diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..ea03939 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,128 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +code@nytimes.com. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..0d9a3bb --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,40 @@ +# Introduction + +Thank you for considering contributing to the grocery module. + +Following these guidelines helps to communicate that you respect the time of the developers managing and developing this open source project. In return, they should reciprocate that respect in addressing your issue, assessing changes, and helping you finalize your pull requests. + +There are many ways to contribute, from writing tutorials or blog posts, improving the documentation, submitting bug reports and feature requests or writing code which can be incorporated into the loader itself. + +# Ground Rules + +- Keep feature versions as small as possible, preferably one new feature per version. +- Refer to [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md) + +# Getting started + +Build instructions are available in the main [README](./README.md). To contribute, follow these steps: + +1. Create your own fork of the code. + +2. Perform the changes in your fork (**tests and documentation are appreciated**). + +3. If you like the change and think the project could use it. Send a pull request. Thank you! + +# How to report a bug + +If you find a security vulnerability, do NOT open an issue. Please report the vulnerability to the person or team maintaining the code. (refer to [SECURITY.md](./SECURITY.md)). + +When filing an issue, make sure to answer these five questions: + +1. What version of Redis are you using? +2. What OS and device are you using? +3. What did you do? +4. What did you expect to see? +5. What did you see instead? + +# How to suggest a feature or enhancement + +If you find yourself wishing for a feature that doesn't exist in grocery, you are probably not alone. There are bound to be others out there with similar needs. Open an issue on our issues list on GitHub which describes the feature you would like to see, why you need it, and how it should work. + +Thank you for contributing! diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f49a4e1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..fe598b5 --- /dev/null +++ b/README.md @@ -0,0 +1,105 @@ +# grocery [![Build Status](https://drone.dv.nyt.net/api/badges/nytimes/grocery/status.svg)](https://drone.dv.nyt.net/nytimes/grocery) [![Go Reference](https://pkg.go.dev/badge/github.com/nytimes/grocery.svg)](https://pkg.go.dev/github.com/nytimes/grocery) + +

+ +

+ +**Grocery** is a framework for simple object storage with Redis in Go. + +This package adds helpful primitives on top of the popular [go-redis](https://github.com/go-redis/redis) framework to store and load structs with ease. Within these structs, Grocery provides built-in support for storing lists, sets, pointers to other structs, and other custom datatypes. + +## Install + +```bash +$ go get github.com/nytimes/grocery +``` + +## Test + +To run tests, you must first have a Redis server running on `localhost:6379`. If you have Docker installed, you may start one with `docker run -dp 6379:6379 redis`. + +```bash +$ go test +``` + +## Example + +```go +package main + +import ( + "fmt" + "sync" + + "github.com/go-redis/redis/v8" + "github.com/nytimes/grocery" +) + +// Supplier specifies how suppliers should be stored in Redis. +type Supplier struct { + // Provides createdAt, updatedAt, and ID + grocery.Base + + // All primitive types are supported. Grocery uses the field's name as the + // key, with the first letter lowercased. Without grocery, this value could + // later be retrieved with "HGET supplier: name" + Name string +} + +// Fruit specifies how fruits should be stored in Redis. +type Fruit struct { + grocery.Base + Name string + + // If another key is desired, it can be specified with a field tag. + // This value can be retrieved with "HGET fruit: cost" + Price float64 `grocery:"cost"` + + // Basic structures such as maps and lists are supported out of the + // box as well. Maps are stored as their own key, so this value can be + // retrieved with "HGETALL fruit::metadata" + Metadata *grocery.Map + + // Pointers to other structs are supported. In Redis, the ID to the + // struct is stored as a string. When this fruit is loaded with + // grocery.Load, it will load the supplier struct as well. + Supplier *Supplier +} + +func main() { + grocery.Init(&redis.Options{ + Addr: "localhost:6379", + }) + + supplier := &Supplier{ + Name: "R&D Fruit Co.", + } + + id, _ := grocery.Store(supplier) + fmt.Printf("Stored at supplier:%s\n", id) + + kv := sync.Map{} + kv.Store("weight", 4.6) + + fruit := &Fruit{ + Name: "mango", + Metadata: &grocery.Map{Map: kv}, + Supplier: supplier, + } + + id, _ := grocery.Store(fruit) + fmt.Printf("Stored at fruit:%s\n", id) +} +``` + +## Contributing + +Refer to [CONTRIBUTING.md](./CONTRIBUTING.md) for general contribution instructions. + +## License + +grocery is available under the Apache 2.0 license. See the LICENSE file for details. + +--- + +> This repository is maintained by the Research & Development team at The New York Times and is provided as-is for your own use. For more information about R&D at the Times visit [rd.nytimes.com](https://rd.nytimes.com) diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..02db605 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,7 @@ +# Security Issues + +The New York Times takes security bugs in the NYTimes authored or maintained OSS seriously. We appreciate your efforts to responsibly disclose your findings, and will make every effort to acknowledge your contributions. + +To report a security issue, email rd@nytimes.com with security bug title as a subject line and you will get a response indicating the next steps in handing your report. + +If you find vulnerabilities in software, libraries or modules used by NYTimes authored or maintained OSS but written by a third-party, please report the vulnerability to the person or team maintaining the code. diff --git a/base.go b/base.go new file mode 100644 index 0000000..8d838c7 --- /dev/null +++ b/base.go @@ -0,0 +1,20 @@ +package grocery + +import ( + "time" +) + +// Base provides default fields for every object stored with grocery. +// +// Unlike most fields, the fields contained within Base are special and should +// not be modified manually, as they are updated by grocery automatically. +type Base struct { + // The timestamp at which the object was created. + CreatedAt time.Time `json:"createdAt" grocery:"createdAt,immutable"` + + // The timestamp at which the object was last modified. + UpdatedAt time.Time `json:"updatedAt" grocery:"updatedAt,immutable"` + + // The object's unique ID. + ID string `json:"id,omitempty" grocery:"-"` +} diff --git a/bind.go b/bind.go new file mode 100644 index 0000000..87ec73f --- /dev/null +++ b/bind.go @@ -0,0 +1,339 @@ +package grocery + +import ( + "errors" + "reflect" + "strconv" + "strings" + "time" +) + +var mapType = reflect.TypeOf(&Map{}) +var setType = reflect.TypeOf(&Set{}) + +// bind is used to load keys and values from data into ptr. Generally, data is +// the result of C.HGetAll(prefix + ":" + id), and ptr is a pointer to a +// struct that has been set up for usage with grocery. +func bind(prefix, id string, data map[string]string, ptr interface{}) error { + if ptr == nil { + return errors.New("ptr must not be nil") + } else if reflect.TypeOf(ptr).Kind() != reflect.Ptr || reflect.TypeOf(ptr).Elem().Kind() != reflect.Struct { + return errors.New("ptr must be a struct pointer") + } else if len(data) == 0 { + return errors.New("data must not be empty") + } + + typ := reflect.TypeOf(ptr).Elem() + val := reflect.ValueOf(ptr).Elem() + return bindStruct(prefix, id, data, typ, val) +} + +func bindStruct(prefix, id string, data map[string]string, typ reflect.Type, val reflect.Value) error { + for i := 0; i < typ.NumField(); i++ { + typeField := typ.Field(i) + structField := val.Field(i) + + // Skip unexported fields + if !structField.CanSet() { + continue + } + + inputFieldName := typeField.Tag.Get("grocery") + + if inputFieldName == "-" { + // Skip values that shouldn't be stored + continue + } else if structField.Kind() == reflect.Struct && typeField.Anonymous { + // Recurse on embedded structs + bindStruct(prefix, id, data, typeField.Type, structField) + continue + } else if strings.Contains(inputFieldName, ",") { + inputFieldName = strings.Split(inputFieldName, ",")[0] + } else if inputFieldName == "" { + // Tag was not specified, assume field name + if len(typeField.Name) > 1 { + inputFieldName = strings.ToLower(string(typeField.Name[0])) + string(typeField.Name[1:]) + } else { + inputFieldName = strings.ToLower(typeField.Name) + } + } + + switch typeField.Type.Kind() { + case reflect.Ptr: + // New item to set in struct + res := reflect.New(typeField.Type.Elem()) + + if typeField.Type == mapType || (typeField.Type.Elem().Kind() == reflect.Struct && res.Elem().FieldByName("CustomMapType").IsValid()) { + // This is a map + m, err := C.HGetAll(ctx, prefix+":"+id+":"+inputFieldName).Result() + + if err != nil { + return err + } + + for k := range m { + res.MethodByName("Store").Call([]reflect.Value{ + reflect.ValueOf(k), + reflect.ValueOf(m[k]), + }) + } + + if setupMethod := res.MethodByName("Setup"); setupMethod.IsValid() { + setupMethod.Call([]reflect.Value{}) + } + + structField.Set(res) + } else if typeField.Type == setType || (typeField.Type.Elem().Kind() == reflect.Struct && res.Elem().FieldByName("CustomSetType").IsValid()) { + // This is a set + s, err := C.SMembers(ctx, prefix+":"+id+":"+inputFieldName).Result() + + if err != nil { + return err + } + + for _, val := range s { + res.MethodByName("Add").Call([]reflect.Value{ + reflect.ValueOf(val), + }) + } + + if setupMethod := res.MethodByName("Setup"); setupMethod.IsValid() { + setupMethod.Call([]reflect.Value{}) + } + + structField.Set(res) + } else if typeField.Type.Elem().Kind() == reflect.Struct { + if _, ok := typeField.Type.Elem().FieldByName("Base"); ok { + // This is a reference to a model that we should load from Redis + id := data[inputFieldName] + + // Load data from redis + subPrefix := strings.ToLower(res.Type().Elem().Name()) + dat, err := C.HGetAll(ctx, subPrefix+":"+id).Result() + + if err != nil { + return err + } else if len(dat) == 0 { + continue + } + + err = bindStruct(subPrefix, id, dat, res.Type().Elem(), res.Elem()) + + if err != nil { + return err + } + + // Set ID + val := res.Elem() + fi := reflect.Indirect(val).FieldByName("ID") + fi.SetString(id) + + structField.Set(res) + } else { + return errors.New("Can't set unsupported struct with key " + inputFieldName) + } + } else { + inputValue, exists := data[inputFieldName] + + if !exists { + continue + } + + if err := setFieldWithKind(typeField.Type.Kind(), inputValue, structField); err != nil { + return err + } + } + case reflect.Slice: + ids, err := C.LRange(ctx, prefix+":"+id+":"+inputFieldName, 0, -1).Result() + + if err != nil { + return err + } + + arr := reflect.MakeSlice(typeField.Type, 0, len(ids)) + + for _, itemID := range ids { + if typeField.Type.Elem().Kind() == reflect.String { + // This is just a string slice + arr = reflect.Append(arr, reflect.ValueOf(itemID)) + } else if _, ok := typeField.Type.Elem().Elem().FieldByName("Base"); ok { + // This is a reference to a model that we should load from Redis + ptr := reflect.New(typeField.Type.Elem().Elem()) + + // Load data from redis + subPrefix := strings.ToLower(ptr.Type().Elem().Name()) + dat, err := C.HGetAll(ctx, subPrefix+":"+itemID).Result() + + if err != nil { + return err + } + + err = bindStruct(subPrefix, itemID, dat, ptr.Type().Elem(), ptr.Elem()) + + if err != nil { + return err + } + + // Set ID + val := ptr.Elem() + fi := reflect.Indirect(val).FieldByName("ID") + fi.SetString(itemID) + + arr = reflect.Append(arr, ptr) + } else { + return errors.New("Can't set unsupported struct") + } + } + + structField.Set(arr) + case reflect.Bool: + if loadFunc := structField.MethodByName("Load"); loadFunc.IsValid() { + // Load custom boolean values + ret := loadFunc.Call([]reflect.Value{ + reflect.ValueOf(id), + reflect.ValueOf(inputFieldName), + }) + + val := strconv.FormatBool(ret[0].Bool()) + + if !ret[1].IsNil() { + return ret[1].Interface().(error) + } + + if err := setFieldWithKind(structField.Kind(), val, structField); err != nil { + return err + } + } else { + inputValue, exists := data[inputFieldName] + + if !exists { + continue + } + + if err := setFieldWithKind(typeField.Type.Kind(), inputValue, structField); err != nil { + return err + } + } + default: + inputValue, exists := data[inputFieldName] + + if !exists { + continue + } + + if err := setFieldWithKind(typeField.Type.Kind(), inputValue, structField); err != nil { + return err + } + } + } + + return nil +} + +func setFieldWithKind(valueKind reflect.Kind, val string, structField reflect.Value) error { + switch valueKind { + case reflect.Ptr: + return setFieldWithKind(structField.Elem().Kind(), val, structField.Elem()) + case reflect.Int: + return setIntField(val, 0, structField) + case reflect.Int8: + return setIntField(val, 8, structField) + case reflect.Int16: + return setIntField(val, 16, structField) + case reflect.Int32: + return setIntField(val, 32, structField) + case reflect.Int64: + return setIntField(val, 64, structField) + case reflect.Uint: + return setUintField(val, 0, structField) + case reflect.Uint8: + return setUintField(val, 8, structField) + case reflect.Uint16: + return setUintField(val, 16, structField) + case reflect.Uint32: + return setUintField(val, 32, structField) + case reflect.Uint64: + return setUintField(val, 64, structField) + case reflect.Bool: + return setBoolField(val, structField) + case reflect.Float32: + return setFloatField(val, 32, structField) + case reflect.Float64: + return setFloatField(val, 64, structField) + case reflect.String: + structField.SetString(val) + case reflect.Struct: + switch structField.Type() { + case reflect.TypeOf(time.Now()): + timeInt, _ := strconv.Atoi(val) + timeVal := time.Unix(int64(timeInt), 0) + structField.Set(reflect.ValueOf(timeVal)) + default: + return errors.New("unknown type") + } + default: + return errors.New("unknown type") + } + + return nil +} + +func setIntField(value string, bitSize int, field reflect.Value) error { + if value == "" { + value = "0" + } + + val, err := strconv.ParseInt(value, 10, bitSize) + + if err != nil { + return err + } + + field.SetInt(val) + return nil +} + +func setUintField(value string, bitSize int, field reflect.Value) error { + if value == "" { + value = "0" + } + + val, err := strconv.ParseUint(value, 10, bitSize) + + if err != nil { + return err + } + + field.SetUint(val) + return nil +} + +func setBoolField(value string, field reflect.Value) error { + if value == "" { + value = "false" + } + + val, err := strconv.ParseBool(value) + + if err != nil { + return err + } + + field.SetBool(val) + return nil +} + +func setFloatField(value string, bitSize int, field reflect.Value) error { + if value == "" { + value = "0.0" + } + + val, err := strconv.ParseFloat(value, bitSize) + + if err != nil { + return err + } + + field.SetFloat(val) + return nil +} diff --git a/bind_test.go b/bind_test.go new file mode 100644 index 0000000..f339121 --- /dev/null +++ b/bind_test.go @@ -0,0 +1,283 @@ +package grocery + +import ( + "strconv" + "testing" + "time" +) + +type CustomString string +type CustomInt int +type CustomFloat float64 + +const ( + customStringVal CustomString = "asdf" + customIntVal CustomInt = 5 + customFloatVal CustomFloat = 3.2 +) + +type BindTestEmbeddedStruct struct { + ES string `grocery:"es"` +} + +type bindTestStruct struct { + // Embedded struct + BindTestEmbeddedStruct + + // Primitives + S string `grocery:"s"` + P *string `grocery:"p"` + I int `grocery:"i"` + I8 int8 `grocery:"i8"` + I16 int16 `grocery:"i16"` + I32 int32 `grocery:"i32"` + I64 int64 `grocery:"i64"` + U uint `grocery:"u"` + U8 uint8 `grocery:"u8"` + U16 uint16 `grocery:"u16"` + U32 uint32 `grocery:"u32"` + U64 uint64 `grocery:"u64"` + F32 float32 `grocery:"f32"` + F64 float64 `grocery:"f64"` + B bool `grocery:"b"` + + // Custom types + CS CustomString `grocery:"cs"` + CI CustomInt `grocery:"ci"` + CF CustomFloat `grocery:"cf"` + + // Timestamp + T time.Time `grocery:"t"` + + // Don't store + NoStore string + NoStoreDash string `grocery:"-"` + + // Can't set unexported values + unexported string `grocery:"us"` +} + +func TestEmbedding(t *testing.T) { + data := map[string]string{ + "es": "asdf", + } + + ptr := new(bindTestStruct) + err := bind("", "", data, ptr) + + if err != nil { + t.Errorf("embedding FAILED, got error %v", err) + } + + if data["es"] == ptr.ES { + t.Log("embedded string PASSED") + } else { + t.Errorf("embedded string FAILED, expected %v but got %v", data["es"], ptr.ES) + } +} + +func TestPrimitives(t *testing.T) { + data := map[string]string{ + "s": "asdf", + "p": "asdfg", + "i": "-4", + "i8": "-34", + "i16": "-5432", + "i32": "-235873", + "i64": "-4300000000", + "u": "4", + "u8": "34", + "u16": "65432", + "u32": "235873", + "u64": "4300000000", + "f32": "329.14", + "f64": "-2190.3895", + "b": "true", + } + + ptr := new(bindTestStruct) + ptr.P = new(string) + + err := bind("", "", data, ptr) + + if err != nil { + t.Errorf("primitives FAILED, got error %v", err) + } + + if data["s"] != ptr.S { + t.Errorf("string FAILED, expected %v but got %v", data["s"], ptr.S) + } + + if data["p"] != *ptr.P { + t.Errorf("string pointer FAILED, expected %v but got %v", data["p"], *ptr.P) + } + + iVal, _ := strconv.ParseInt(data["i"], 10, 64) + if int(iVal) != ptr.I { + t.Errorf("int FAILED, expected %v but got %v", data["i"], ptr.I) + } + + i8Val, _ := strconv.ParseInt(data["i8"], 10, 8) + if int8(i8Val) != ptr.I8 { + t.Errorf("int8 FAILED, expected %v but got %v", data["i8"], ptr.I8) + } + + i16Val, _ := strconv.ParseInt(data["i16"], 10, 16) + if int16(i16Val) != ptr.I16 { + t.Errorf("int16 FAILED, expected %v but got %v", data["i16"], ptr.I16) + } + + i32Val, _ := strconv.ParseInt(data["i32"], 10, 32) + if int32(i32Val) != ptr.I32 { + t.Errorf("int32 FAILED, expected %v but got %v", data["i32"], ptr.I32) + } + + i64Val, _ := strconv.ParseInt(data["i64"], 10, 64) + if int64(i64Val) != ptr.I64 { + t.Errorf("int64 FAILED, expected %v but got %v", data["i64"], ptr.I64) + } + + uVal, _ := strconv.ParseUint(data["u"], 10, 64) + if uint(uVal) != ptr.U { + t.Errorf("uint FAILED, expected %v but got %v", data["u"], ptr.U) + } + + u8Val, _ := strconv.ParseUint(data["u8"], 10, 8) + if uint8(u8Val) != ptr.U8 { + t.Errorf("uint8 FAILED, expected %v but got %v", data["u8"], ptr.U8) + } + + u16Val, _ := strconv.ParseUint(data["u16"], 10, 16) + if uint16(u16Val) != ptr.U16 { + t.Errorf("uint16 FAILED, expected %v but got %v", data["u16"], ptr.U16) + } + + u32Val, _ := strconv.ParseUint(data["u32"], 10, 32) + if uint32(u32Val) != ptr.U32 { + t.Errorf("uint32 FAILED, expected %v but got %v", data["u32"], ptr.U32) + } + + u64Val, _ := strconv.ParseUint(data["u64"], 10, 64) + if uint64(u64Val) != ptr.U64 { + t.Errorf("uint64 FAILED, expected %v but got %v", data["u64"], ptr.U64) + } + + f32Val, _ := strconv.ParseFloat(data["f32"], 32) + if float32(f32Val) != ptr.F32 { + t.Errorf("float32 FAILED, expected %v but got %v", data["f32"], ptr.F32) + } + + f64Val, _ := strconv.ParseFloat(data["f64"], 64) + if float64(f64Val) != ptr.F64 { + t.Errorf("float64 FAILED, expected %v but got %v", data["f64"], ptr.F64) + } + + bVal, _ := strconv.ParseBool(data["b"]) + if bVal != ptr.B { + t.Errorf("bool FAILED, expected %v but got %v", data["b"], ptr.B) + } +} + +func TestCustomMapType(t *testing.T) { + data := map[string]string{ + "cs": string(customStringVal), + "ci": strconv.Itoa(int(customIntVal)), + "cf": strconv.FormatFloat(float64(customFloatVal), 'f', 10, 64), + } + + ptr := new(bindTestStruct) + err := bind("", "", data, ptr) + + if err != nil { + t.Errorf("custom map type FAILED, got error %v", err) + } + + if data["cs"] == string(ptr.CS) { + t.Log("custom string PASSED") + } else { + t.Errorf("custom string FAILED, expected %v but got %v", data["cs"], ptr.CS) + } + + ciVal, _ := strconv.ParseInt(data["ci"], 10, 64) + if int(ciVal) == int(ptr.CI) { + t.Log("custom int PASSED") + } else { + t.Errorf("custom int FAILED, expected %v but got %v", data["ci"], ptr.CI) + } + + cfVal, _ := strconv.ParseFloat(data["cf"], 64) + if cfVal == float64(ptr.CF) { + t.Log("custom float PASSED") + } else { + t.Errorf("custom float FAILED, expected %v but got %v", data["cf"], ptr.CF) + } +} + +func TestTimestamp(t *testing.T) { + data := map[string]string{ + "t": "963210120", + } + + ptr := new(bindTestStruct) + err := bind("", "", data, ptr) + + if err != nil { + t.Errorf("timestamp FAILED, got error %v", err) + } + + tVal, _ := strconv.ParseInt(data["t"], 10, 64) + if tVal == ptr.T.Unix() { + t.Log("timestamp PASSED") + } else { + t.Errorf("timestamp FAILED, expected %v but got %v", data["t"], ptr.T) + } +} + +func TestInvalidValue(t *testing.T) { + data := map[string]string{ + "i": "asdf", + } + + ptr := new(bindTestStruct) + err := bind("", "", data, ptr) + + if err != nil { + t.Log("invalid value PASSED") + } else { + t.Error("invalid value FAILED, expecting error") + } +} + +func TestInvalidPointer(t *testing.T) { + data := map[string]string{ + "s": "asdf", + } + + err := bind("", "", data, nil) + + if err != nil { + t.Log("nil pointer PASSED") + } else { + t.Error("nil pointer FAILED, expecting error") + } + + err = bind("", "", data, 4) + + if err != nil { + t.Log("nil pointer PASSED") + } else { + t.Error("nil pointer FAILED, expecting error") + } +} + +func TestEmptyData(t *testing.T) { + emptyData := map[string]string{} + ptr := new(bindTestStruct) + err := bind("", "", emptyData, ptr) + + if err != nil { + t.Log("empty data PASSED") + } else { + t.Error("empty data FAILED, expecting error") + } +} diff --git a/connector.go b/connector.go new file mode 100644 index 0000000..2179f92 --- /dev/null +++ b/connector.go @@ -0,0 +1,129 @@ +package grocery + +import ( + "context" + "sync" + "time" + + "github.com/go-redis/redis/v8" +) + +var ( + // The underlying Redis client powering grocery. Use this field to run your + // own Redis commands. + C *redis.Client + + // Context for all Redis queries, currently unused. + ctx = context.Background() + + // Callback functions that listen for events published to Redis. + handlers = make(map[string][]func(string, []byte)) + + // Handler synchronization. + handlersMux sync.RWMutex + + // Redis channel to send a test message on during initialization. + firstMessageChannel = "hello_world" + + // Channel used as a signal to indicate PSubscribe is ready. + firstMessageSignal = make(chan bool) + + // Set to true when the first message goes through pub/sub successfully. + receivedFirstMessage = false + + // Persistent pubsub connection that waits for published events. + psc *redis.PubSub +) + +// Init initializes the Redis client and additionally starts a pub/sub client. +func Init(config *redis.Options) error { + C = redis.NewClient(config) + + if _, err := C.Ping(ctx).Result(); err != nil { + return err + } + + go listenForUpdates() + + // Repeatedly send messages while we wait for listenForUpdates to start + // listening + go func() { + for range time.Tick(time.Millisecond * 10) { + if receivedFirstMessage { + return + } + + C.Publish(ctx, firstMessageChannel, "") + } + }() + + // Wait until we can confirm listenForUpdates is working before returning + <-firstMessageSignal + + return nil +} + +func listenForUpdates() { + psc = C.PSubscribe(ctx, "*") + + for { + msg, err := psc.ReceiveMessage(ctx) + + if !receivedFirstMessage && msg.Channel == firstMessageChannel { + // Send signal on first message to confirm PSubscribe is ready + firstMessageSignal <- true + receivedFirstMessage = true + continue + } + + if err != nil { + // Panic if we disconnect from Redis + panic("Disconnected from redis") + } + + handlersMux.RLock() + handlers, ok := handlers[msg.Channel] + handlersMux.RUnlock() + + if !ok { + // Received message for a channel that nobody is subscribed to + continue + } + + for _, handler := range handlers { + handler(msg.Channel, []byte(msg.Payload)) + } + } +} + +// Subscribe adds a new listener function to a channel in our pub/sub +// connection. For example, if you want to listen to events on the 'reset' +// channel, and then publish a test event, you might do the following: +// +// db.Subscribe([]string{"reset"}, func(channel string, payload []byte) { +// fmt.Println("receiving data from " + channel) +// }) +// +// db.C.Publish("reset", "payload") +func Subscribe(channels []string, handler func(string, []byte)) { + handlersMux.Lock() + defer handlersMux.Unlock() + + for _, channel := range channels { + if _, ok := handlers[channel]; !ok { + handlers[channel] = []func(string, []byte){} + } + + handlers[channel] = append(handlers[channel], handler) + } +} + +// Unsubscribe removes all listeners waiting on any channel in channels. +func Unsubscribe(channels []string) { + handlersMux.Lock() + defer handlersMux.Unlock() + + for _, channel := range channels { + delete(handlers, channel) + } +} diff --git a/custom_bool_test.go b/custom_bool_test.go new file mode 100644 index 0000000..b2f9b3b --- /dev/null +++ b/custom_bool_test.go @@ -0,0 +1,62 @@ +package grocery + +import ( + "testing" + + "github.com/go-redis/redis/v8" +) + +type ZContains bool + +func (ZContains) Load(itemID, fieldName string) (bool, error) { + _, err := C.ZRank(ctx, fieldName, itemID).Result() + return err == nil, nil +} + +type ZContainsTestModel struct { + Base + + Name string `grocery:"name"` + IsMember ZContains `grocery:"sortedSetTest"` +} + +func TestZContains(t *testing.T) { + model := &ZContainsTestModel{ + Name: "hello world", + } + + model2 := &ZContainsTestModel{ + Name: "hello world 2", + } + + id, err := Store(model) + id2, _ := Store(model2) + + if err != nil { + t.Error(err) + } + + C.ZAdd(ctx, "sortedSetTest", &redis.Z{Score: 1, Member: id}).Result() + + loadedModel := new(ZContainsTestModel) + err = Load(id, loadedModel) + + loadedModel2 := new(ZContainsTestModel) + Load(id2, loadedModel2) + + if err != nil { + t.Error(err) + } + + if !loadedModel.IsMember { + t.Errorf("zcontains FAILED, expected %t but got %t", true, loadedModel.IsMember) + } + + if loadedModel2.IsMember { + t.Errorf("zcontains FAILED, expected %t but got %t", false, loadedModel2.IsMember) + } + + if loadedModel.Name != model.Name { + t.Errorf("zcontains name FAILED, expected %s but got %s", model.Name, loadedModel.Name) + } +} diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..6bb8a21 --- /dev/null +++ b/doc.go @@ -0,0 +1,7 @@ +// Package grocery provides helper functions for storing objects in Redis. +// +// This package adds helpful primitives on top of the popular go-redis +// framework to store and load structs with ease. Within these structs, +// Grocery provides built-in support for storing lists, sets, pointers to other +// structs, and other custom datatypes. +package grocery diff --git a/docs/demo.go.txt b/docs/demo.go.txt new file mode 100644 index 0000000..cbc1941 --- /dev/null +++ b/docs/demo.go.txt @@ -0,0 +1,71 @@ +// Code used in the video demo +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" + "time" + + "github.com/fatih/color" + "github.com/go-redis/redis/v8" + "github.com/nytimes/grocery" +) + +type Item struct { + grocery.Base + Name string + Price float64 +} + +func readLine() string { + reader := bufio.NewReader(os.Stdin) + text, _ := reader.ReadString('\n') + return strings.Replace(text, "\n", "", -1) +} + +func delay_print(s string, c *color.Color) { + for _, char := range s { + c.Print(string(char)) + time.Sleep(50 * time.Millisecond) + } +} + +func main() { + magenta := color.New(color.Bold).Add(color.FgHiMagenta) + red := color.New(color.Bold).Add(color.FgHiRed) + cyan := color.New(color.Bold).Add(color.FgHiCyan) + reset := color.New(color.Reset) + + delay_print("Grocery", magenta) + delay_print(" is a framework for simple object storage with ", reset) + delay_print("Redis", red) + delay_print(" in ", reset) + delay_print("Go", cyan) + delay_print(".\n\nLet’s use it to store an item in our inventory:\n", reset) + + grocery.Init(&redis.Options{ + Addr: "localhost:6379", + }) + + fmt.Print("Item name: ") + name := readLine() + + fmt.Print("Item price: ") + price, _ := strconv.ParseFloat(readLine(), 64) + + item := &Item{ + Name: name, + Price: price, + } + + id, err := grocery.Store(item) + + if err != nil { + panic(err) + } + + fmt.Printf("Stored at item:%s\n", id) +} diff --git a/docs/gopher.png b/docs/gopher.png new file mode 100644 index 0000000..b21f1be Binary files /dev/null and b/docs/gopher.png differ diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 0000000..53c3caf --- /dev/null +++ b/docs/index.html @@ -0,0 +1,315 @@ + + + + + + Grocery + + + +
+ + + + + + + + +
+ +
+

+ Grocery is a framework for simple object storage with Redis in Go. +

+

+ Features include: +

+
    +
  • Easy usage of structs as object models
  • +
  • Built-in support for lists, sets, and object pointers
  • +
  • Flexible custom datatypes
  • +
+ GitHub + Docs +
+ +
+
+ +
+ +
package main
+
+import (
+	"fmt"
+	"sync"
+
+	"github.com/go-redis/redis/v8"
+	"github.com/nytimes/grocery"
+)
+
+// Supplier specifies how suppliers should be stored in Redis.
+type Supplier struct {
+	// Provides createdAt, updatedAt, and ID
+	grocery.Base
+
+	// All primitive types are supported. Grocery uses the field's name as the
+	// key, with the first letter lowercased. Without grocery, this value could
+	// later be retrieved with "HGET supplier: name"
+	Name string
+}
+
+// Fruit specifies how fruits should be stored in Redis.
+type Fruit struct {
+	grocery.Base
+	Name string
+
+	// If another key is desired, it can be specified with a field tag.
+	// This value can be retrieved with "HGET fruit: cost"
+	Price float64 `grocery:"cost"`
+
+	// Basic structures such as maps and lists are supported out of the
+	// box as well. Maps are stored as their own key, so this value can be
+	// retrieved with "HGETALL fruit::metadata"
+	Metadata *grocery.Map
+
+	// Pointers to other structs are supported. In Redis, the ID to the
+	// struct is stored as a string. When this fruit is loaded with
+	// grocery.Load, it will load the supplier struct as well.
+	Supplier *Supplier
+}
+
+func main() {
+	grocery.Init(&redis.Options{
+		Addr: "localhost:6379",
+	})
+
+	supplier := &Supplier{
+		Name: "R&D Fruit Co.",
+	}
+
+	id, _ := grocery.Store(supplier)
+	fmt.Printf("Stored at supplier:%s\n", id)
+
+	kv := sync.Map{}
+	kv.Store("weight", 4.6)
+
+	fruit := &Fruit{
+		Name: "mango",
+		Metadata: &grocery.Map{Map: kv},
+		Supplier: supplier,
+	}
+
+	id, _ := grocery.Store(fruit)
+	fmt.Printf("Stored at fruit:%s\n", id)
+}
+
+ + + + + + + + + + + + + + diff --git a/docs/output.gif b/docs/output.gif new file mode 100644 index 0000000..eab4891 Binary files /dev/null and b/docs/output.gif differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5f0a0cb --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module github.com/nytimes/grocery + +go 1.18 + +require ( + github.com/go-redis/redis/v8 v8.11.5 + github.com/google/uuid v1.3.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + golang.org/x/net v0.0.0-20220420153159-1850ba15e1be // indirect + golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ae799e3 --- /dev/null +++ b/go.sum @@ -0,0 +1,19 @@ +github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= +github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= +golang.org/x/net v0.0.0-20220420153159-1850ba15e1be h1:yx80W7nvY5ySWpaU8UWaj5o9e23YgO9BRhQol7Lc+JI= +golang.org/x/net v0.0.0-20220420153159-1850ba15e1be/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/load.go b/load.go new file mode 100644 index 0000000..682f54d --- /dev/null +++ b/load.go @@ -0,0 +1,99 @@ +package grocery + +import ( + "errors" + "reflect" + "strings" + + "github.com/go-redis/redis/v8" +) + +// Load automates the process of loading data from Redis, binding it to a +// struct, and then setting base attributes (such as ID). See Store for more +// information on creating structs for grocery. Load can be used like so: +// +// type Item struct { +// grocery.Base +// Name string `grocery:"name"` +// } +// +// itemID := "asdf" +// item := new(Item) +// db.Load(itemID, item) +func Load(id string, ptr interface{}) error { + if reflect.TypeOf(ptr).Kind() != reflect.Ptr || reflect.TypeOf(ptr).Elem().Kind() != reflect.Struct { + return errors.New("ptr must be a struct pointer") + } + + // Get prefix for the struct (e.g. 'answer:' from Answer) + prefix := strings.ToLower(reflect.TypeOf(ptr).Elem().Name()) + + // Load object data + res, _ := C.HGetAll(ctx, prefix + ":" + id).Result() + + if err := bind(prefix, id, res, ptr); err != nil { + return err + } + + // Set the ID before returning + val := reflect.ValueOf(ptr) + fi := reflect.Indirect(val).FieldByName("ID") + fi.SetString(id) + + // Call post-load hook + postLoad := reflect.ValueOf(ptr).MethodByName("PostLoad") + + if postLoad.IsValid() { + postLoad.Call([]reflect.Value{}) + } + + return nil +} + +// LoadAll automates the process of running multiple Load calls through a +// pipeline. If you're fetching multiple objects from Redis, you should +// generally use LoadAll instead of calling Load multiple times. Read more +// about pipelining at https://redis.io/topics/pipelining. +func LoadAll[T any](ids []string, values *[]T) error { + if len(ids) != len(*values) { + return errors.New("len(ids) must equal len(*values)") + } else if len(ids) == 0 { + return errors.New("len(ids) must be greater than zero") + } + + // Get prefix for the struct (e.g. 'answer:' from Answer) + prefix := strings.ToLower(reflect.ValueOf(values).Elem().Index(0).Type().Name()) + + // Pipeline all HGetAll commands + pip := C.Pipeline() + cmds := make([]*redis.StringStringMapCmd, len(ids)) + + for i, id := range ids { + cmds[i] = pip.HGetAll(ctx, prefix + ":" + id) + } + + pip.Exec(ctx) + + for i, cmd := range cmds { + res, _ := cmd.Result() + itemPtr := &((*values)[i]) + + if err := bind(prefix, ids[i], res, itemPtr); err != nil { + return err + } + + // Set ID + val := reflect.ValueOf(itemPtr).Elem() + fi := reflect.Indirect(val).FieldByName("ID") + fi.SetString(ids[i]) + + // Call post-load hook + postLoad := reflect.ValueOf(itemPtr).MethodByName("PostLoad") + + if postLoad.IsValid() { + postLoad.Call([]reflect.Value{}) + } + } + + return nil +} diff --git a/load_test.go b/load_test.go new file mode 100644 index 0000000..5ab67a9 --- /dev/null +++ b/load_test.go @@ -0,0 +1,149 @@ +package grocery + +import ( + "testing" + "time" +) + +type StringAlias string + +type LoadTestModel struct { + Base + + StringVal string + IntVal int + UInt16Val uint16 + BoolVal bool + Float32Val float32 + Float64Val float64 + + StringAliasVal StringAlias + MapVal *Map + SetVal *Set +} + +func TestLoad(t *testing.T) { + mapData := map[string]string{"a": "b", "c": "d"} + setData := []string{"a", "b"} + + model := &LoadTestModel{ + StringVal: "hello world", + IntVal: 4, + UInt16Val: 3, + BoolVal: true, + Float32Val: 3.5, + Float64Val: 3.9, + StringAliasVal: "asdf", + MapVal: newMap(mapData), + SetVal: newSet(setData), + } + + id, err := Store(model) + storeTime := time.Now() + + if err != nil { + t.Error(err) + } + + loadedModel := new(LoadTestModel) + err = Load(id, loadedModel) + + if err != nil { + t.Error(err) + } + + if loadedModel.ID != id { + t.Errorf("id FAILED, expected %s but got %s", id, model.ID) + } + + if time.Since(loadedModel.CreatedAt) < time.Since(storeTime) { + t.Errorf("createdAt FAILED") + } + + if time.Since(loadedModel.UpdatedAt) < time.Since(storeTime) { + t.Errorf("updatedAt FAILED") + } + + if model.StringVal != loadedModel.StringVal { + t.Errorf("string FAILED, expected %s but got %s", model.StringVal, loadedModel.StringVal) + } + + if model.IntVal != loadedModel.IntVal { + t.Errorf("int FAILED, expected %d but got %d", model.IntVal, loadedModel.IntVal) + } + + if model.UInt16Val != loadedModel.UInt16Val { + t.Errorf("uint16 FAILED, expected %d but got %d", model.UInt16Val, loadedModel.UInt16Val) + } + + if model.BoolVal != loadedModel.BoolVal { + t.Errorf("bool FAILED, expected %t but got %t", model.BoolVal, loadedModel.BoolVal) + } + + if model.Float32Val != loadedModel.Float32Val { + t.Errorf("float32 FAILED, expected %f but got %f", model.Float32Val, loadedModel.Float32Val) + } + + if model.Float64Val != loadedModel.Float64Val { + t.Errorf("float64 FAILED, expected %f but got %f", model.Float32Val, loadedModel.Float32Val) + } + + if model.StringAliasVal != loadedModel.StringAliasVal { + t.Errorf("string alias FAILED, expected %s but got %s", model.StringAliasVal, loadedModel.StringAliasVal) + } + + for k, v := range mapData { + loadedValue, ok := loadedModel.MapVal.Load(k) + + if !ok { + t.Errorf("map value FAILED, key %s does not exist", k) + continue + } + + if v != loadedValue.(string) { + t.Errorf("map value FAILED, expected m[%s]=%s but got m[%s]=%s", k, v, k, loadedValue.(string)) + } + } + + for _, k := range setData { + if !loadedModel.SetVal.Contains(k) { + t.Errorf("set value FAILED, key %s does not exist", k) + } + } +} + +func TestLoadMissing(t *testing.T) { + model := new(LoadTestModel) + err := Load("asdf", model) + + if err == nil { + t.Error("Should throw error for missing models but did not") + } +} + +func TestLoadAll(t *testing.T) { + model := &LoadTestModel{ + StringVal: "hello world", + IntVal: 4, + BoolVal: true, + StringAliasVal: "asdf", + MapVal: newMap(map[string]string{"a": "b", "c": "d"}), + } + + id, err := Store(model) + + if err != nil { + t.Error(err) + } + + loadedModels := make([]LoadTestModel, 1) + err = LoadAll([]string{id}, &loadedModels) + + if err != nil { + t.Error(err) + } + + if loadedModels[0].ID != id { + t.Errorf("id FAILED, expected %s but got %s", id, model.ID) + } +} diff --git a/map.go b/map.go new file mode 100644 index 0000000..5c903d4 --- /dev/null +++ b/map.go @@ -0,0 +1,120 @@ +package grocery + +import ( + "encoding/json" + "sync" +) + +// Map is used to represent maps stored in Redis. See CustomMapType and Store +// for more information. +type Map struct { + sync.Map +} + +// CustomMapType must be embedded by any struct that is used as a model's field +// and should be represented as a map in Redis. The parent struct must also +// embed grocery.Map. As a motivating example, let's imagine we want to store +// projects in Redis, and each project should contain a map that stores user +// IDs along with each user's role for that project. +// +// type User struct { +// grocery.Base +// Name string `grocery:"name"` +// +// // Role is not stored in Redis because it is specific to each project +// // the user is part of. +// Role string `grocery:"-"` +// } +// +// type Project struct { +// grocery.Base +// Users *UserRolesMap `grocery:"users"` +// } +// +// type UserRolesMap struct { +// grocery.CustomMapType +// grocery.Map +// users map[string]*User +// } +// +// func (m *UserRolesMap) Setup() { +// // Setup gets called once the map's contents have already been loaded +// // from Redis. In this function, we have the opportunity to populate the +// // private m.users map with our own User structs, instead of using the +// // underlying grocery.Map (which is a sync.Map internally) to just +// // access user IDs. +// +// userIDs := make([]string, 0, m.Count()) +// +// m.Range(func(key, value interface{}) bool { +// userIDs = append(userIDs, key.(string)) +// return true +// }) +// +// // Load users from Redis +// users := make([]User, len(userIDs)) +// grocery.LoadAll(userIDs, &users) +// +// // Populate m.users +// m.users = make(map[string]*User) +// +// for i := 0; i < len(userIDs); i++ { +// // Use underlying map data to get user's role +// users[i].Role, _ = m.Load(userIDs[i]) +// +// // Then, store retrieved user model in m.users +// m.users[userIDs[i]] = &users[i] +// } +// } +type CustomMapType interface { + // Called once the map's data is loaded from Redis, allowing us to populate + // any custom fields. + Setup() +} + +func newMap(data map[string]string) *Map { + m := &Map{Map: sync.Map{}} + + for k, v := range data { + m.Store(k, v) + } + + return m +} + +// Count returns the number of items stored in the map. +func (m *Map) Count() int { + num := 0 + + m.Range(func(key, value any) bool { + num += 1 + return true + }) + + return num +} + +func (m *Map) MarshalJSON() ([]byte, error) { + res := make(map[string]string) + + m.Range(func(key, value interface{}) bool { + res[key.(string)] = value.(string) + return true + }) + + return json.Marshal(res) +} + +func (m *Map) UnmarshalJSON(data []byte) error { + res := make(map[string]string) + + if err := json.Unmarshal(data, &res); err != nil { + return err + } + + for k, v := range res { + m.Store(k, v) + } + + return nil +} diff --git a/map_test.go b/map_test.go new file mode 100644 index 0000000..647f735 --- /dev/null +++ b/map_test.go @@ -0,0 +1,37 @@ +package grocery + +import ( + "encoding/json" + "testing" +) + +func TestMap(t *testing.T) { + mapData := map[string]string{"a": "b", "c": "d"} + m := newMap(mapData) + + m.Map.Range(func(key, value interface{}) bool { + loadedValue, ok := mapData[key.(string)] + + if !ok { + t.Errorf("map value FAILED, key %s does not exist", key) + return true + } + + if value != loadedValue { + t.Errorf("map value FAILED, expected m[%s]=%s but got m[%s]=%s", key, value, key, loadedValue) + } + + return true + }) + + if len(mapData) != m.Count() { + t.Errorf("map count FAILED, expected %d but got %d", len(mapData), m.Count()) + } + + expectedJSON, _ := json.Marshal(mapData) + json, _ := json.Marshal(m) + + if string(json) != string(expectedJSON) { + t.Errorf("marshaljson FAILED, expected %s but got %s", string(expectedJSON), string(json)) + } +} diff --git a/model_hook.go b/model_hook.go new file mode 100644 index 0000000..8c30f1a --- /dev/null +++ b/model_hook.go @@ -0,0 +1,13 @@ +package grocery + +import "github.com/go-redis/redis/v8" + +// ModelHook provides hooks that structs may implement if you would like to +// receive callbacks on certain events. +type ModelHook interface { + // PostStore is called after all necessary commands to store the object + // have been passed to a pipeline. The pipeline is provided here in case + // you would like to pass any additional commands to Redis, to be executed + // immediately after the store completes. + PostStore(pip redis.Pipeliner) +} diff --git a/set.go b/set.go new file mode 100644 index 0000000..460e35d --- /dev/null +++ b/set.go @@ -0,0 +1,90 @@ +package grocery + +import ( + "sync" +) + +// Set is used to represent sets stored in Redis. See CustomSetType and Store +// for more information. +type Set struct { + sync.Map +} + +// CustomSetType must be embedded by any struct that is used as a model's field +// and should be represented as a set in Redis. The parent struct must also +// embed grocery.Set. As a motivating example, let's imagine we want to store +// projects in Redis, and each project should contain a set that stores users +// who have access to that project. However, when the project is marshaled into +// JSON, we only want to reveal the number of users who have access, and not +// the identities of the users themselves. +// +// type User struct { +// grocery.Base +// Name string `grocery:"name"` +// } +// +// type Project struct { +// grocery.Base +// Users *UsersSet `grocery:"users"` +// } +// +// type UsersSet struct { +// grocery.CustomSetType +// grocery.Set +// } +// +// func (s *UsersSet) Setup() { +// // Setup gets called once the set's contents have already been loaded +// // from Redis. For this example, we don't need to do anything, but this +// // function allows for lots of flexibility. See CustomMapType for more +// // details. +// } +// +// func (s *UsersSet) MarshalJSON() { +// // Only marshal the size of the set, as opposed to all of the set's +// // contents. +// return json.Marshal(s.Cardinality()) +// } +type CustomSetType interface { + Setup() +} + +func newSet(data []string) *Set { + s := &Set{Map: sync.Map{}} + + for _, k := range data { + s.Store(k, 0) + } + + return s +} + +// Add adds a key k to the set. +func (s *Set) Add(k any) { + s.Store(k, 0) +} + +// Contains returns true if the set contains k. +func (s *Set) Contains(k any) bool { + _, ok := s.Load(k) + return ok +} + +// Cardinality returns the size of the set. +func (s *Set) Cardinality() int { + num := 0 + + s.Range(func(key, value any) bool { + num += 1 + return true + }) + + return num +} + +// Iter calls f once for each key in the set. +func (s *Set) Iter(f func(k any) bool) { + s.Range(func(k, value any) bool { + return f(k) + }) +} diff --git a/store.go b/store.go new file mode 100644 index 0000000..f463d59 --- /dev/null +++ b/store.go @@ -0,0 +1,42 @@ +package grocery + +import ( + "github.com/google/uuid" +) + +// StoreOptions provides options that may be passed to StoreWithOptions if the +// default behavior of Store needs to be changed. +type StoreOptions struct { + // ID is the ID this object must be stored with. Store will fail if an + // object with the type of the pointer being passed already exists with + // this ID, unless Overwrite is set to true. + ID string + + // Set to true if you would like to overwrite existing objects stored with + // this ID. + Overwrite bool + + // All other options inherit from UpdateOptions. + *UpdateOptions +} + +// Store saves an object in Redis. As with all other Grocery operations, the +// name of the pointer's struct type is used as a prefix for the object's key. +// The object's ID is then randomly generated, and the object is stored at +// prefix:id. If you would like to set a specific ID, use StoreWithOptions. +func Store(ptr interface{}) (string, error) { + id := uuid.NewString() + return id, StoreWithOptions(ptr, &StoreOptions{id, false, nil}) +} + +// StoreWithOptions saves an object in Redis, like Store, but with options. +func StoreWithOptions(ptr interface{}, opts *StoreOptions) error { + if opts.UpdateOptions == nil { + opts.UpdateOptions = &UpdateOptions{} + } + + opts.UpdateOptions.isStore = true + opts.UpdateOptions.storeOverwrite = opts.Overwrite + + return updateInternal(opts.ID, ptr, opts.UpdateOptions) +} diff --git a/store_ref_test.go b/store_ref_test.go new file mode 100644 index 0000000..ce01ccf --- /dev/null +++ b/store_ref_test.go @@ -0,0 +1,74 @@ +package grocery + +import ( + "testing" +) + +type A struct { + Base + Name string `grocery:"name"` +} + +type B struct { + Base + A *A `grocery:"ref"` +} + +type Ctest struct { + Base + As []*A `grocery:"arrRef"` +} + +func TestStoreReference(t *testing.T) { + a := &A{Name: "bob"} + aID, err := Store(a) + + if err != nil { + t.Error(err) + } + + b := &B{A: a} + bID, err := Store(b) + + if err != nil { + t.Error(err) + } + + loadedB := new(B) + Load(bID, loadedB) + + if loadedB.A.ID != aID { + t.Errorf("ref FAILED, expected %s but got %s", aID, loadedB.A.ID) + } + + if loadedB.A.Name != a.Name { + t.Errorf("ref name FAILED, expected %s but got %s", a.Name, loadedB.A.Name) + } +} + +func TestStoreListReference(t *testing.T) { + a := &A{Name: "bob"} + aID, err := Store(a) + + if err != nil { + t.Error(err) + } + + c := &Ctest{As: []*A{a}} + cID, err := Store(c) + + if err != nil { + t.Error(err) + } + + loadedC := new(Ctest) + Load(cID, loadedC) + + if loadedC.As[0].ID != aID { + t.Errorf("ref FAILED, expected %s but got %s", aID, loadedC.As[0].ID) + } + + if loadedC.As[0].Name != a.Name { + t.Errorf("ref name FAILED, expected %s but got %s", a.Name, loadedC.As[0].Name) + } +} diff --git a/test_test.go b/test_test.go new file mode 100644 index 0000000..22cc20c --- /dev/null +++ b/test_test.go @@ -0,0 +1,60 @@ +package grocery + +import ( + "context" + "os" + "testing" + + "github.com/go-redis/redis/v8" +) + +var keys = map[string]bool{} + +func TestMain(m *testing.M) { + // Create grocery client + Init(&redis.Options{ + Addr: "localhost:6379", + }) + + // Add hook for saving keys + C.AddHook(testHook{}) + + // Run unit test + code := m.Run() + + // Delete all keys saved to Redis while running unit tests + pip := C.Pipeline() + + for id := range keys { + pip.Del(ctx, id) + } + + pip.Exec(ctx) + + // Exit from test + os.Exit(code) +} + +// Hooks used to save keys stored during tests so we can delete them once tests complete +type testHook struct{} + +func (testHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) { + return ctx, nil +} + +func (testHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error { + keys[cmd.Args()[1].(string)] = true + return nil +} + +func (testHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) { + return ctx, nil +} + +func (testHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error { + for _, cmd := range cmds { + keys[cmd.Args()[1].(string)] = true + } + + return nil +} diff --git a/update.go b/update.go new file mode 100644 index 0000000..b66bf06 --- /dev/null +++ b/update.go @@ -0,0 +1,224 @@ +package grocery + +import ( + "errors" + "fmt" + "reflect" + "strings" + "time" + + "github.com/go-redis/redis/v8" +) + +// UpdateOptions provides options that may be passed to UpdateWithOptions if +// the default behavior of Update needs to be changed. +type UpdateOptions struct { + // SetZeroValues should be set to true if you would like to update all zero + // values in Redis (e.g. empty strings, 0 ints). By default, when creating + // a struct to pass to Update, you may not set each value, which is why + // this defaults to false. At this time, setting one zero value at a time + // is not supported. + SetZeroValues bool + + // If you would like to run this store/update alongside other Redis + // updates, you may specify a pipeline. + Pipeline redis.Pipeliner + + isStore bool + storeOverwrite bool +} + +// Update updates an object with a given ID. By default, only non-zero values +// are updated in Redis, allowing you to update one property at a time. +// +// type Item struct { +// grocery.Base +// Name string +// Price float64 +// } +// +// item := &Item{ +// Price: 5.64, +// } +// +// // Name is not updated, but price is +// itemID := "asdf" +// db.Update(itemID, item) +func Update(id string, ptr interface{}) error { + return updateInternal(id, ptr, &UpdateOptions{}) +} + +// UpdateWithOptions updates an object in Redis, like Update, but with options. +func UpdateWithOptions(id string, ptr interface{}, opts *UpdateOptions) error { + return updateInternal(id, ptr, opts) +} + +func updateInternal(id string, ptr interface{}, opts *UpdateOptions) error { + if reflect.TypeOf(ptr).Kind() != reflect.Ptr || reflect.TypeOf(ptr).Elem().Kind() != reflect.Struct { + return errors.New("ptr must be a struct pointer") + } + + if id == "" { + return errors.New("ID must not be empty") + } + + // Get prefix for the struct (e.g. 'answer:' from Answer) + prefix := strings.ToLower(reflect.TypeOf(ptr).Elem().Name()) + + // Make sure the object exists on an update, or not on a store + exists, _ := C.Exists(ctx, prefix+":"+id).Result() + + if opts.isStore && exists == 1 && !opts.storeOverwrite { + return fmt.Errorf("%s:%s already exists", prefix, id) + } else if !opts.isStore && exists == 0 { + return fmt.Errorf("%s:%s does not exist", prefix, id) + } + + val := reflect.ValueOf(ptr).Elem() + typ := val.Type() + pip := opts.Pipeline + + if opts.Pipeline == nil { + pip = C.Pipeline() + } + + for i := 0; i < typ.NumField(); i++ { + typeField := typ.Field(i) + structField := val.Field(i) + + tagName := typeField.Tag.Get("grocery") + k := tagName + + if tagName == "-" { + continue + } else if structField.Kind() == reflect.Struct && typeField.Anonymous { + // Skip embedded structs + continue + } else if strings.Contains(tagName, ",") { + tagParts := strings.Split(tagName, ",") + k = tagParts[0] + tagInfo := tagParts[1] + + if tagInfo == "immutable" { + continue + } + } else if tagName == "" { + // Tag was not specified, assume field name + if len(typeField.Name) > 1 { + k = strings.ToLower(string(typeField.Name[0])) + string(typeField.Name[1:]) + } else { + k = strings.ToLower(typeField.Name) + } + } + + if !opts.SetZeroValues && structField.IsZero() { + continue + } + + switch typeField.Type.Kind() { + case reflect.Ptr: + if structField.IsNil() { + continue + } + + if typeField.Type == mapType || structField.Elem().FieldByName("CustomMapType").IsValid() { + pip.Del(ctx, prefix+":"+id+":"+k) + + structField.MethodByName("Range").Call([]reflect.Value{ + reflect.ValueOf(func(key, value interface{}) bool { + pip.HSet(ctx, prefix+":"+id+":"+k, key, value) + return true + }), + }) + } else if typeField.Type == setType || structField.Elem().FieldByName("CustomSetType").IsValid() { + pip.Del(ctx, prefix+":"+id+":"+k) + + structField.MethodByName("Range").Call([]reflect.Value{ + reflect.ValueOf(func(key, value interface{}) bool { + pip.SAdd(ctx, prefix+":"+id+":"+k, key) + return true + }), + }) + } else if !structField.Elem().FieldByName("Base").IsZero() { + val := structField.Elem().FieldByName("Base").FieldByName("ID").String() + pip.HSet(ctx, prefix+":"+id, k, val) + } else { + return fmt.Errorf("Can't set unknown field '%s'", tagName) + } + case reflect.Slice: + for i := 0; i < structField.Len(); i++ { + if !structField.Index(i).Elem().FieldByName("Base").IsZero() { + itemID := structField.Index(i).Elem().FieldByName("Base").FieldByName("ID").String() + pip.RPush(ctx, prefix+":"+id+":"+k, itemID) + } else { + return fmt.Errorf("Can't set unknown array item in %s", tagName) + } + } + case reflect.Struct: + switch structField.Type() { + case reflect.TypeOf(time.Now()): + timeVal := structField.MethodByName("Unix").Call([]reflect.Value{})[0].Int() + pip.HSet(ctx, prefix+":"+id, k, timeVal) + default: + return fmt.Errorf("Can't set unknown struct for field '%s'", tagName) + } + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + // Handle int alias types + val := structField.Convert(reflect.TypeOf(0)).Int() + pip.HSet(ctx, prefix+":"+id, k, val) + case reflect.String: + // Handle string alias types + val := structField.Convert(reflect.TypeOf("")).String() + pip.HSet(ctx, prefix+":"+id, k, val) + case reflect.Bool: + if loadFunc := structField.MethodByName("Load"); loadFunc.IsValid() { + // Skip custom boolean values; they don't get stored + continue + } else { + pip.HSet(ctx, prefix+":"+id, k, structField.Bool()) + } + case reflect.Float64, reflect.Float32: + val := structField.Float() + pip.HSet(ctx, prefix+":"+id, k, val) + case reflect.Interface: + if structField.Type().Name() == "ModelHook" { + // Skip ModelHook fields + continue + } + + return fmt.Errorf("Don't know how to set interface field '%s'", k) + default: + return fmt.Errorf("Don't know how to set field '%s'", k) + } + } + + // Set updatedAt timestamp + pip.HSet(ctx, prefix+":"+id, "updatedAt", time.Now().Unix()) + + if opts.isStore { + // Set ID + fi := reflect.Indirect(val).FieldByName("ID") + fi.SetString(id) + + // Set createdAt timestamp + pip.HSet(ctx, prefix+":"+id, "createdAt", time.Now().Unix()) + + // Call hook after calling store, if the object has one + if hook, ok := ptr.(ModelHook); ok { + hook.PostStore(pip) + } + } + + // Don't exec if a pipeline was provided to us + if opts.Pipeline == nil { + if _, err := pip.Exec(ctx); err != nil { + return err + } + + if err := Load(id, ptr); err != nil { + return err + } + } + + return nil +} diff --git a/update_test.go b/update_test.go new file mode 100644 index 0000000..2515663 --- /dev/null +++ b/update_test.go @@ -0,0 +1,88 @@ +package grocery + +import ( + "testing" + "time" +) + +type UpdateTestModel struct { + Base + + StringVal string + TimeVal time.Time +} + +func TestSetZeroValues(t *testing.T) { + m := &UpdateTestModel{ + StringVal: "asdf", + } + + id, err := Store(m) + + if err != nil { + t.Error(err) + } + + // Check 0 + model := new(UpdateTestModel) + err = Load(id, model) + + if err != nil { + t.Error(err) + } + + if model.StringVal != m.StringVal { + t.Errorf("TestSetZeroValues FAILED, initial value was not set correctly") + } + + // Update 1 + m.StringVal = "" + err = Update(id, m) + + if err != nil { + t.Error(err) + } + + // Check 1 + Load(id, model) + + if model.StringVal == "" { + t.Errorf("TestSetZeroValues FAILED, zero value was set") + } + + // Update 2 + m.StringVal = "" + err = UpdateWithOptions(id, m, &UpdateOptions{ + SetZeroValues: true, + }) + + // Check 2 + Load(id, model) + + if model.StringVal != "" { + t.Errorf("TestSetZeroValues FAILED, zero value was not set") + } +} + +func TestSetTimeValue(t *testing.T) { + m := &UpdateTestModel{ + TimeVal: time.Now(), + } + + id, err := Store(m) + + if err != nil { + t.Error(err) + } + + model := new(UpdateTestModel) + err = Load(id, model) + + if err != nil { + t.Error(err) + } + + if model.TimeVal != m.TimeVal { + t.Errorf("TestSetTimeValue FAILED, initial value was not set correctly") + } +}