jsontrans

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ModelNameToTypeStringMapping = make(map[string]string)

ModelNameToTypeStringMapping maps name of the IModel to TypeString This is for "href" field of the JSON output Both pointer and nonpointer type names are stored

Functions

func ContainsIFieldTransformModelToJSON added in v0.4.13

func ContainsIFieldTransformModelToJSON(f *JSONFields) bool

ContainsIFieldTransformModelToJSON check if f contains any struct comforms to IFieldTransformModelToJSON

func TransFromByHidingDateFieldsFromIModel

func TransFromByHidingDateFieldsFromIModel(v reflect.Value, includeCUDDates bool) map[string]interface{}

TransFromByHidingDateFieldsFromIModel does some default transformation the package provides Without user defining a model comforming to IHasPermissions interface It doesn't respect all json tag, but naming and omitempty does The reason we choose to traverse IModel is because I want to fill the "href" field This picks all the stuff from the IModel to a map We don't run JSON marshalling library this stage. It is expect it to be run on the map

func Transform

func Transform(data map[string]interface{}, f *JSONFields, permType Permission) (map[string]interface{}, error)

Transform performs the following: 1. Cherry pick only fields we want by the fields specifier when given a json string, I cannot cherry pick struct, because I can't make it generic enough to fit all mdl Then that means I have to loop the array to json marshal it 2. Transform the value if necessary (used to be called JSONCherryPickFields)

Types

type Field

type Field int

Field specify what to do with each field

const (
	FieldNone Field = iota
	FieldOmitEmpty
	FieldIncludeEmpty // for black list which default to block
	FieldBlock        // block when it's blacklist
	FieldPass         // right now set it as nil is the same as pass
)

type IFieldTransformJSONToModel added in v0.4.13

type IFieldTransformJSONToModel interface {
	TransformJSONToModel(field interface{}) (interface{}, error)
}

IFieldTransformJSONToModel does any transformation from JSON when fetching from user JSON input

type IFieldTransformModelToJSON added in v0.4.13

type IFieldTransformModelToJSON interface {
	TransformModelToJSON(field interface{}) (interface{}, error)
}

IFieldTransformModelToJSON does any transformation from model to JSON when fetching out of database

type JSONFieldBlock

type JSONFieldBlock bool

JSONFieldBlock is defined for field that should be blocked when PermissionBlackList is used TODO: doesn't work yet

type JSONFieldEmptyList

type JSONFieldEmptyList bool

JSONFieldEmptyList is defined for field should be an empty list if no value exists TODO: need to check if this works

type JSONFieldEmptyString

type JSONFieldEmptyString bool

JSONFieldEmptyString is defined for field should be an empty string if no value exists TODO: need to check if this works

type JSONFields

type JSONFields map[string]interface{}

JSONFields is the fields we specify to pick out only the JSON fields we want

type Permission

type Permission int

Permission of the JSON parsing

const (
	// PermissionWhiteList means blocking all fields by default blocked, only specify those that are there
	PermissionWhiteList Permission = iota
	// PermissionBlackList means allow fields by default allowed, but make transformation to certain field
	// including to block it (not supported yet)
	PermissionBlackList
)

Jump to

Keyboard shortcuts

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