Code Monkey home page Code Monkey logo

scoped's Introduction

scoped

A simple way to remove fields from your JSON API by using scope tags inside the golang struct.

Example Struct

Notice the scope tags for each field in the struct. Think of it as permissions...

type Example struct {
    ID           int64  `json:"id"`
    AdminOnlyStr string `json:"admin_only" scope:"admin"`
    UserOnlyStr  string `json:"user_only,omitempty" scope:"user"`
    BothStr      string `json:"both,omitempty" scope:"user,admin"`
    OmitStr      string `json:"omiter,omitempty" scope:"user,admin"`
    Hidden       string `json:"-"`
    All          string `json:"all,omitempty"`
}

Usage

example := Example{
	ID:           1,
	AdminOnlyStr: "im an admin",
	UserOnlyStr:  "im a user",
	BothStr:      "im on both",
	Hidden:       "cant see this",
	All:          "should always be known",
}

out := scoped.New("admin", example)

Easy right? Based on the Example struct above, and the value for it, the scoped package will remove values inside this struct that DO NOT have the admin scope.

Scoped HTTP Handler

For ease of use, scoped has a http.Handler so you can just put the scope right in there.

func NewServer() {
	r := mux.NewRouter()
	r.Handle("/api/orders", scoped.Handler("admin", OrdersHandler))
	http.ListenAndServe(":8080", r)
}

func OrdersHandler(w http.ResponseWriter, r *http.Request) interface{} {
	adminOrders := database.FetchOrders()
	return adminOrders
}

scoped's People

Contributors

hunterlong avatar

Stargazers

Doru Carastan avatar

Watchers

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