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
- func FromContext(ctx context.Context) (websiteID, storeID uint32, ok bool)
- func Valid(s string) bool
- func ValidBytes(b []byte) bool
- func ValidParent(current Type, parent Type) bool
- func WithContext(ctx context.Context, websiteID, storeID uint32) context.Context
- type Perm
- func (bits Perm) All() Perm
- func (bits Perm) Has(s Type) bool
- func (bits Perm) Human(ret ...string) []string
- func (bits Perm) MarshalJSON() ([]byte, error)
- func (bits Perm) Set(scopes ...Type) Perm
- func (bits Perm) String() string
- func (bits Perm) Top() Type
- func (bits *Perm) UnmarshalJSON(data []byte) error
- type Type
- type TypeID
- func (t TypeID) AppendBytes(dst []byte) (text []byte)
- func (t TypeID) AppendHuman(dst []byte, separator byte) (text []byte)
- func (t TypeID) EqualTypes(other TypeID) bool
- func (t TypeID) GoString() string
- func (t TypeID) ID() (uint32, error)
- func (t TypeID) IsValid() error
- func (t TypeID) MarshalBinary() (data []byte, err error)
- func (t TypeID) MarshalText() (text []byte, err error)
- func (t TypeID) Segment() uint8
- func (t TypeID) String() string
- func (t TypeID) ToIntString() string
- func (t TypeID) ToUint64() uint64
- func (t TypeID) Type() Type
- func (t *TypeID) UnmarshalBinary(data []byte) error
- func (t *TypeID) UnmarshalText(text []byte) error
- func (t TypeID) Unpack() (s Type, id uint32)
- func (t TypeID) ValidParent(parent TypeID) bool
- type TypeIDs
- type TypeStr
Constants ¶
const DefaultTypeID = TypeID(Default)<<24 | 0
DefaultTypeID default Hash value for Default Scope and ID 0. Avoids typing
scope.NewHash(DefaultID,0)
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.
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 ¶
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 ¶
Valid checks if s is a valid StrScope of either StrDefault, StrWebsites or StrStores. Case-sensitive. Input should all be lowercase.
func ValidBytes ¶
ValidBytes checks if b is a valid byte Type of either StrDefault, StrWebsites or StrStores. Case-sensitive.
func ValidParent ¶
ValidParent validates if the parent scope is within the hierarchical chain: default -> website -> store.
func WithContext ¶
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.
PermDefault convenient helper contains default scope permission level.
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.
PermStoreReverse convenient helper to enforce hierarchy levels. Only used in config.Scoped implementation.
PermWebsite convenient helper contains default and website scope permission levels.
PermWebsiteReverse convenient helper to enforce hierarchy levels. Only used in config.Scoped implementation.
func MakePerm ¶
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) Has ¶
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) MarshalJSON ¶
MarshalJSON implements json.Marshaler
func (Perm) Top ¶
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 ¶
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.
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 ¶
FromBytes returns the Type from a byte slice. Supported values are default, websites, stores, Default, Website, Group and store. Case sensitive.
func FromString ¶
FromString returns the Type from a string: default, websites or stores. Opposite of FromType.
func (Type) IsValid ¶
IsValid checks if the type is within the scope Default, Website, Group or Store.
func (Type) IsWebSiteOrStore ¶
IsWebSiteOrStore returns true if the type is either Website or Store.
func (Type) MarshalJSON ¶
MarshalJSON implements the Marshaler interface. The returned byte slice is owned by the callee. You must copy it for further use.
func (Type) StrBytes ¶
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 ¶
StrType converts the underlying Type to one of the three available type strings from the database table `core_config_data`.
func (*Type) UnmarshalJSON ¶
UnmarshalJSON implements the Unmarshaler interface
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 ¶
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 ¶
MakeTypeIDString creates a TypeID from an uint base 10 numbered string.
func (TypeID) AppendBytes ¶
AppendBytes appends the uint32 to the destination bytes `dst`.
func (TypeID) AppendHuman ¶
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 ¶
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) ID ¶
ID returns the underlying assigned ID. If the ID overflows the MaxID or is smaller than zero then it returns 0.
func (TypeID) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler
func (TypeID) MarshalText ¶
MarshalText implements encoding.TextMarshaler
func (TypeID) Segment ¶
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) ToIntString ¶
ToIntString converts the TypeID to a stringyfied number representation.
func (*TypeID) UnmarshalBinary ¶
UnmarshalBinary implements encoding.BinaryUnmarshaler
func (*TypeID) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler
func (TypeID) Unpack ¶
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 ¶
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) Lowest ¶
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) TargetAndParents ¶
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.