Coder Social home page Coder Social logo

pinned's Introduction

pinned

This is a proof-of-concept, date based versioning system for APIs inspired by Stripe's API versioning.

Build Status GoDoc Go Report Card

Overview

This package enables reverse compatibility for a Go API by defining versions and their associated changes. Consequently, API versions can be maintained for long periods of time without minimal effort.

Usage

See the included example project for detailed usage.

Versioning is done at a resource/struct level. If a type implements Versionable it can take advantage of this package.

  1. To start, create a new VersionManager.
vm := &pinned.VersionManager{
  Layout: "2006-01-02",
  Header: "API Version",
}
  1. Then add Versions.
// Initial version.
vm.Add(&pinned.Version{
  Date: "2018-02-10",
})

// New version.
vm.Add(&pinned.Version{
  Date: "2018-02-11",
  Changes: []*pinned.Change{
    &pinned.Change{
      Description: "New things",
      Actions: map[string]pinned.Action{
        "Object": someMethod,
      }
    }
  }
})

someMethod is applied to all type Object, and has the signature func(map[string]interface{}) map[string]interface{}.

  1. Handle an incoming request.
func handler(w http.ResponseWriter, r *http.Request) {
  // Get version from request.
  v, _ := vm.Parse(r)

  // Set version in context.
  ctx = pinned.NewContext(r.Context(), v)
  
  // ...Fetch resources...

  // Apply version changes to resources.
  body, _ := vm.Apply(v, data)

  // Write response.
  data, err := json.Marshal(body)
  if err != nil {
    panic(err)
  }

  w.Header().Set("Content-Type", "application/json")
  w.Write(data)
}

Example

Consider the following simple example. An API has a User struct which looks like this:

type User struct {
  ID       uint64
  FullName string
}

Now we decide we want to rename FullName to Name. However, this is a breaking change. To ensure stability, prior to change we set a version 2018-02-10.

After the change, we set a version 2018-02-11. This version has a change associated with it. This Change has an Action to be taken on the User resource.

This Action is a func that reverses the change made in the new version.

func userNameFieldChange(mapping map[string]interface{}) map[string]interface{} {
  mapping["full_name"] = mapping["name"]
  delete(mapping, "name")
  return mapping
}

There are now two versions, 2018-02-11 and 2018-02-10. To support the client that requested version 2018-02-10, the "changes" made in version 2018-02-11 are undone, and the User resource now reflects the requested version.

As versions are added, these changes are sequentially undone. This enables a version to be supported for a long period of time, and allows the developer to focus on new feature development without much concern towards legacy versions.

pinned's People

Contributors

sjkaliski avatar dvrkps avatar

Watchers

James Cloos avatar  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.