scope

package
v0.0.0-...-202847b Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2023 License: Apache-2.0 Imports: 9 Imported by: 48

Documentation

Overview

Package scope defines the configuration of scopes default, website, group and store.

Outside package scope we refer to type scope.Type as simple just scope and scope.TypeID as ScopeID.

The fall back explained from bottom to top:

  • +-----------+ | | Default | <---------------+ | +-----------+ | | + | Falls back to | ^ | +------------+ +---------+ | | Websites | | +------------+ <---------+ | + | Falls back to | + | +-----------+ | | | Stores +------+
  • +-----------+ http://asciiflow.com

A group scope does not make sense in the above schema but is supported by other Go types in this package.

Index

Constants

View Source
const DefaultTypeID = TypeID(Default)<<24 | 0

DefaultTypeID default Hash value for Default Scope and ID 0. Avoids typing

scope.NewHash(DefaultID,0)
View Source
const MaxID = 1<<23 - 1

MaxID maximum allowed ID which can be packed into a TypeID. The ID relates to an auto_increment column in the database. Doesn't matter whether we have a website, group or store scope. int24 (8388607) size at the moment.

View Source
const TypeIDMaxSegments = 256

TypeIDMaxSegments maximum supported segments or also known as shards. This constant can be used to create the segmented array in other packages.

Variables

This section is empty.

Functions

func FromContext

func FromContext(ctx context.Context) (websiteID, storeID uint32, ok bool)

FromContext returns the requested current store scope and its parent website scope from a context. This scope is only valid for the current context in a request. A scope gets set via HTTP form, cookie, JSON Web Token or GeoIP or other fancy features.

func Valid

func Valid(s string) bool

Valid checks if s is a valid StrScope of either StrDefault, StrWebsites or StrStores. Case-sensitive. Input should all be lowercase.

func ValidBytes

func ValidBytes(b []byte) bool

ValidBytes checks if b is a valid byte Type of either StrDefault, StrWebsites or StrStores. Case-sensitive.

func ValidParent

func ValidParent(current Type, parent Type) bool

ValidParent validates if the parent scope is within the hierarchical chain: default -> website -> store.

func WithContext

func WithContext(ctx context.Context, websiteID, storeID uint32) context.Context

WithContext adds the store scope with its parent website scope to the context. Different middlewares may call this function to set a new scope depending on different conditions. For example the JSON web token middleware can set a scope because the JWT contains a new store code. Or a geoip middleware can set the scope depending on geo location information. These IDs will be later used to e.g. read the scoped configuration.

Types

type Perm

type Perm uint16

Perm is a bit set and used for permissions depending on the scope.Type. Uint16 should be big enough.

const PermDefault Perm = 1 << Default

PermDefault convenient helper contains default scope permission level.

const PermStore Perm = 1<<Default | 1<<Website | 1<<Store

PermStore convenient helper contains all scope permission levels. The official core_config_data table and its classes to not support the GroupID scope, so that is the reason why PermStore does not have a GroupID.

const PermStoreReverse Perm = 1 << Store

PermStoreReverse convenient helper to enforce hierarchy levels. Only used in config.Scoped implementation.

const PermWebsite Perm = 1<<Default | 1<<Website

PermWebsite convenient helper contains default and website scope permission levels.

const PermWebsiteReverse Perm = 1<<Store | 1<<Website

PermWebsiteReverse convenient helper to enforce hierarchy levels. Only used in config.Scoped implementation.

func MakePerm

func MakePerm(name string) (p Perm, err error)

MakePerm creates a Perm type based on the input argument which can be either: "default","d" or "" for PermDefault, "websites", "website" or "w" for PermWebsite OR "stores", "store" or "s" for PermStore. Any other argument triggers a NotSupported error.

func (Perm) All

func (bits Perm) All() Perm

All applies DefaultID, WebsiteID and StoreID scopes

func (Perm) Has

func (bits Perm) Has(s Type) bool

Has checks if a given scope.Type exists within a Perm. Only the first argument is supported. Providing no argument assumes the scope.DefaultID.

func (Perm) Human

func (bits Perm) Human(ret ...string) []string

Human readable representation of the permissions

func (Perm) MarshalJSON

func (bits Perm) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (Perm) Set

func (bits Perm) Set(scopes ...Type) Perm

Set takes a variadic amount of Group to set them to Bits

func (Perm) String

func (bits Perm) String() string

String readable representation of the permissions

func (Perm) Top

func (bits Perm) Top() Type

Top returns the highest stored scope within a Perm. A Perm can consists of 3 scopes: 1. Default -> 2. Website -> 3. Store Highest scope for a Perm with all scopes is: Store.

func (*Perm) UnmarshalJSON

func (bits *Perm) UnmarshalJSON(data []byte) error

MarshalJSON implements json.Marshaler

type Type

type Type uint8

Type or also known as Scope defines the hierarchy of the overall CoreStore library. The hierarchy chain travels from Default->Website->Group->Store. The type relates to the database tables `website`, `store_group` and `store`. Type is a part of type Perm.

const (
	Absent Type = iota // must start with 0
	Default
	Website
	Group
	Store
)

Those constants define the overall scopes. The hierarchical order is always:

Absent -> Default -> Website -> Group -> Store

These internal IDs may change without notice.

func FromBytes

func FromBytes(b []byte) Type

FromBytes returns the Type from a byte slice. Supported values are default, websites, stores, Default, Website, Group and store. Case sensitive.

func FromString

func FromString(s string) Type

FromString returns the Type from a string: default, websites or stores. Opposite of FromType.

func (Type) IsValid

func (s Type) IsValid() error

IsValid checks if the type is within the scope Default, Website, Group or Store.

func (Type) IsWebSiteOrStore

func (s Type) IsWebSiteOrStore() bool

IsWebSiteOrStore returns true if the type is either Website or Store.

func (Type) MarshalJSON

func (s Type) MarshalJSON() ([]byte, error)

MarshalJSON implements the Marshaler interface. The returned byte slice is owned by the callee. You must copy it for further use.

func (Type) StrBytes

func (s Type) StrBytes() []byte

StrBytes returns the TypeStr as byte slice from a Type. The returned byte slice is owned by the callee. You must copy it for further use.

func (Type) StrType

func (s Type) StrType() string

StrType converts the underlying Type to one of the three available type strings from the database table `core_config_data`.

func (Type) String

func (s Type) String() string

String human readable name of a Type. For Marshaling see Perm.

func (*Type) UnmarshalJSON

func (s *Type) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the Unmarshaler interface

func (Type) WithID

func (s Type) WithID(id uint32) TypeID

WithID calls MakeTypeID for your convenience. It packs the id into a new value containing the Type and its ID ;-).

type TypeID

type TypeID uint32

TypeID defines a merged Scope with its ID. The first 8 bit represents the Type: Default, Website, Group or Store. The last 24 bit represents the assigned ID. This ID relates to the database table in M2 to `website`, `store` or `store_group`. The maximum ID which can be used is defined in constant MaxID.

func MakeTypeID

func MakeTypeID(t Type, id uint32) TypeID

MakeTypeID creates a new merged value of a Type and its ID. An error is equal to returning 0. An error occurs when id is greater than MaxStoreID or smaller 0. An errors occurs when the Scope is Default and ID anything else than 0.

func MakeTypeIDString

func MakeTypeIDString(s string) (TypeID, error)

MakeTypeIDString creates a TypeID from an uint base 10 numbered string.

func (TypeID) AppendBytes

func (t TypeID) AppendBytes(dst []byte) (text []byte)

AppendBytes appends the uint32 to the destination bytes `dst`.

func (TypeID) AppendHuman

func (t TypeID) AppendHuman(dst []byte, separator byte) (text []byte)

AppendHuman appends to dst the human textual representation of a Websites or Stores scope and their IDs. Default, Group and invalid scopes won't get appended. Will write:

scope.Websites.WithID(1) => websites/1
scope.Stores.WithID(2) => stores/2
scope.DefaultTypeID => "" <- returns dst unchanged.
scope.Group.WithID(4) => "" <- returns dst unchanged.

This function gets used in the config package to write a path depending on the paths scope.

func (TypeID) EqualTypes

func (t TypeID) EqualTypes(other TypeID) bool

EqualTypes compares the type of two TypeIDs and returns true if their type matches. This functions checks overflows, would then return false. Two TypeIDs with an Absent type are never equal.

func (TypeID) GoString

func (t TypeID) GoString() string

GoString compilable representation of a hash.

func (TypeID) ID

func (t TypeID) ID() (uint32, error)

ID returns the underlying assigned ID. If the ID overflows the MaxID or is smaller than zero then it returns 0.

func (TypeID) IsValid

func (t TypeID) IsValid() error

IsValid checks if the scope and its ID are valid.

func (TypeID) MarshalBinary

func (t TypeID) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (TypeID) MarshalText

func (t TypeID) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler

func (TypeID) Segment

func (t TypeID) Segment() uint8

Segment generates an 0 < ID <= 255 from a TypeID. Only used within an array index to optimize map[] usage in high concurrent situations. Also known as shard. An array of N shards is created, each shard contains its own instance of the cache with a lock. When an item with unique key needs to be cached a shard for it is chosen at first by the function Segment(). After that the cache lock is acquired and a write to the cache takes place. Reads are analogue.

func (TypeID) String

func (t TypeID) String() string

String human readable output

func (TypeID) ToIntString

func (t TypeID) ToIntString() string

ToIntString converts the TypeID to a stringyfied number representation.

func (TypeID) ToUint64

func (t TypeID) ToUint64() uint64

ToUint64 converts the hash

func (TypeID) Type

func (t TypeID) Type() Type

Type returns the underlying assigned type.

func (*TypeID) UnmarshalBinary

func (t *TypeID) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*TypeID) UnmarshalText

func (t *TypeID) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler

func (TypeID) Unpack

func (t TypeID) Unpack() (s Type, id uint32)

Unpack extracts a Scope and its ID from a hash. Returned ID can be 0 when the Hash contains invalid data. If the data is invalid the Type gets set to `Absent` constant.

func (TypeID) ValidParent

func (t TypeID) ValidParent(parent TypeID) bool

ValidParent validates if the parent Type is within the hierarchical chain: default -> website -> store. Returns also true when parent is zero.

type TypeIDs

type TypeIDs []TypeID

TypeIDs collection of multiple TypeID values.

func (TypeIDs) Len

func (t TypeIDs) Len() int

Len is part of sort.Interface.

func (TypeIDs) Less

func (t TypeIDs) Less(i, j int) bool

Less is part of sort.Interface.

func (TypeIDs) Lowest

func (t TypeIDs) Lowest() (TypeID, error)

Lowest finds from TypeIDs the common lowest Type. All Types must have within their Type the same ID otherwise an error will be returned. This functions gets mainly used in backend* packages if several configuration paths must be applied to one functional option. Eg. config path A has Type Website(1) but config path B has Type Store(2) and config path C has Type Website(1) so the common valid TypeID resolves to Store(2). If there would be a config path with Type Store(3) then a NotValid error gets returned.

func (TypeIDs) String

func (t TypeIDs) String() string

String returns a semi-colon separated list of n TypeID.

func (TypeIDs) Swap

func (t TypeIDs) Swap(i, j int)

Swap is part of sort.Interface.

func (TypeIDs) TargetAndParents

func (t TypeIDs) TargetAndParents() (target TypeID, parents TypeIDs)

TargetAndParents extracts from a given slice the first index (that is zero) as target and removes the first index from the slice to return the parents. target contains either the DefaultTypeID or the desired TypeID. Parents contains at least the DefaultTypeID (appended at the end) and all other parents. But only those parents which are really a parent in the hierarchical order Default->Website->Group->Store. No sorting will be performed on the parents. This function gets mainly used to perform hierarchical look ups with the parents slice in the net packages to create a new scoped configuration for the target TypeID.

type TypeStr

type TypeStr string

TypeStr represents a string Type from table `core_config_data` column scope with special functions attached, mainly for path generation

const (
	StrDefault  TypeStr = strDefault
	StrWebsites TypeStr = strWebsites
	StrStores   TypeStr = strStores
)

Str* constants are used in the database table core_config_data. StrDefault defines the global scope. StrWebsites defines the website scope which has default as parent and stores as child. StrStores defines the store scope which has default and websites as parent.

func FromType

func FromType(scopeID Type) TypeStr

FromType returns the string representation for a Type. Opposite of FromString.

func (TypeStr) String

func (s TypeStr) String() string

String returns the scope as string

func (TypeStr) Type

func (s TypeStr) Type() Type

Type returns the underlying type.

Jump to

Keyboard shortcuts

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