priv

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2018 License: MIT Imports: 4 Imported by: 0

README

priv

Privatize data in your Go (Golang) application

Build Status codecov Maintainability GoDoc

Under development; API may change.

About

Currently priv exports a simple API, which is composed of priv.ToMap and priv.ToMapErr. priv.ToMap panics if a field that does not exist is requested, whereas priv.ToMapErr returns an error. Most of the time panicing is not a big deal, since it would indicate a typo in the code.

These functions allow mapping structs and arrays/slices of structs to map[string]interface{} and []map[string]interface{}{}, respectively. One example of using priv is if you only want to expose certain fields at an API endpoint.

Example

users := [User{ID: "123", Some: {Nested: {Field: "abc"}}, SomethingElse: true}]
v := priv.ToMap(users, "ID", "Some.Nested.Field->Renamed.Location")
// Thus, v = [{ID: "123", Renamed: {Location: "abc"}}]

Documentation

Overview

Package priv focuses on privatizing data in your Go application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToMap

func ToMap(item interface{}, fieldsToInclude ...string) interface{}

ToMap converts the item (struct or array of structs) to a map. Only the fields in the fieldsToInclude list are included. This list supports the dot operator to access nested fields, as well as the -> operator to rename a nested field. This function panics if the field cannot be found or set. If you are calling this function with a dynamically built list of fieldsToInclude, you may want to use the priv.ToMapErr alternative.

eg.

users := [{ID: "123", Some: {Nested: {Field: "abc"}}, SomethingElse: true}]
ToMap(users, "ID", "Some.Nested.Field->Renamed.Location")

would result in:

[{ID: "123", Renamed: {Location: "abc"}}]

func ToMapErr

func ToMapErr(item interface{}, fieldsToInclude ...string) (interface{}, error)

ToMapErr does the same thing as priv.ToMap, but returns an error if the field cannot be found or set. Most of the time panicing (as priv.ToMap does) is fine, since the fields are set before compiling, so a panic to warn of a typo is not a big deal.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL