GithubHelp home page GithubHelp logo

rob05c / apiver Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 16 KB

A Go API versioning package, for encoding and decoding structs per Semantic Versioning.

License: MIT License

Go 100.00%

apiver's Introduction

apiver

A Go API versioning package, for encoding and decoding structs per Semantic Versioning.

This package lets you use Semantic Versioning (https://semver.org/), specifically minor versioning, on a Go API, via struct tags.

To use it, add struct tags for the minor version each field was added in, then call UnmarshalJSON(bytes, object, version) or MarshalJSON(obj, version) with the current version. For example, if an HTTP client requests https://example.com/api/1.1/foo, you would load the Foo object, then call MarshalJSON(foo, 1.1).

The encoding/json functions Marshal, MarshalIndent, Unmarshal, NewDecoder, and NewEncoder are also implemented as object methods, to make it easier to use this package as a drop-in replacement for encoding/json. For example:

json := apiver.NewJSON(1.1)
bts, err := json.Marshal(obj)

Accepting weakly-typed strings for primitives is also supported, via a str tag field. This is different than the encoding/json tag field ,string, which will only decode strings. This str tag field accepts both strings and the native primitive type (integers, unsigned integers, floats, or bools). The encoding/json ,string tag field will also encode the value as a string, whereas apiver ,str will encode the value as a JSON number or boolean.

Example struct:

	type Obj struct {
		Foo int      `json:"foo" api:"1.1,str"`
		A   int      `json:"a" api:"1.1,str"`
		F   *float32 `json:"f" api:"1.4,str"`
	}

For more examples, see the tests.

Other Encodings

Currently, only JSON marshal and unmarshal functions are provided. But the package is structured such that adding additional encodings would be relatively easy. The functions takes real objects, parse their tags, dynamically create new objects with the appropriate fields and tags, then pass them to encoding/json Marshal and Unmarshal.

Hence, to add another encoding, the new functions need only call the object construction functions and pass the result to their encoder and decoder functions. See BuildUnmarshalObj, FromUnmarshalObj, and BuildMarshalObj.

Tests

The package currently has 130 tests in 1104 lines, and go test -cover reports 90.7%.

Comparatively, the functions which make up the core reflection logic comprise 238 lines, and the total project including encoding/json wrappers is 392 lines of code.

apiver's People

Contributors

rob05c avatar

Watchers

 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.