context

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: Apache-2.0 Imports: 19 Imported by: 53

README

godoc Go Report Card

context - the weaving together of data in structure

This package primarily provides a simple map type, called "Context":

type Context map[string]interface{}

The Context can be used like a normal map type, which also means that these sorts of things are not concurrecy-safe. The purpose of these structures are intended for ephemeral cases such as the unmarshalling of JSON data and accessing the information as conveniently as possible.

Like most Go-CoreLibs, Go-Curses and Go-Enjin projects, Context follows a developer-focused design - "make it nice to work with". However we need to define what "nice" means in this context because of the limitations of human language.

A project is defined as "nice to work with" when (in no particular order):

  • it works well with the Go standard library
  • it does not make decisions for the developer
  • it is convenient to type into any text editor
  • it is convenient to conceptualize it's use cases
  • it is designed with a balance of security and convenience
  • it does not require arcane, occult or estoric knowledge to use effectively

What is a Context anyways?

Merriam-Webster has a lovely blurb on the meaning of the word "context", copied here (without permission) because of it's relevance and because I love their work:

Did you know?

In its earliest uses (documented in the 15th century), context meant "the
weaving together of words in language." This sense, now obsolete, developed
logically from the word's source in Latin, contexere "to weave or join
together." Context now most commonly refers to the environment or setting in
which something (whether words or events) exists. When we say that something
is contextualized, we mean that it is placed in an appropriate setting, one in
which it may be properly considered.

-- https://www.merriam-webster.com/dictionary/context -- please support your favorite dictionary, even if it's not Merriam-Webster -- the languages we use in life defines the limits of what can be thought of

For this project in specific, the meaning of the word "context" is actually both the obsolete and modern meanings.

Within the Go-Enjin project, where this package manifested first, there are a number of use cases:

  • it is the front-matter portion of any parsed page
  • it is the means for parsing page editor form submissions
  • it is a type used for unmarshalling JSON data
  • it is a type used for scanning DB query rows

Now that this project has been migrated from it's original home at: github.com/go-enjin/be/pkg/context to this new home as a formal Go-CoreLibs project, this package will end up being used anytime a type for managing the weaving together of data in structure makes sense.

Installation

> go get github.com/go-corelibs/context@latest

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2024 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package context provides a means for the weaving together of data in structure

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context map[string]interface{}

Context is a convenient map[string]interface{} type, used by Go-Enjin for representing page front-matter and other contextual cases

Context is not a replacement for the standard Go context library

func New

func New() (ctx Context)

New constructs a new Context instance

func NewFromMap

func NewFromMap(m map[string]interface{}) Context

NewFromMap deep copies and transforms the given map into a Context with all values converted to Context types

func NewFromOsEnviron

func NewFromOsEnviron(environs ...[]string) (c Context)

NewFromOsEnviron constructs a new Context from os.Environ() string K=V slices, unquoting any quoted values, all the keys will be exactly as present in the input environ strings

func ParseJson

func ParseJson(content string) (m Context, err error)

func ParseToml

func ParseToml(content string) (m Context, err error)

func ParseYaml

func ParseYaml(content string) (m Context, err error)

func (Context) Apply

func (c Context) Apply(contexts ...Context)

Apply takes a list of contexts and merges their contents into this one

func (Context) ApplySpecific

func (c Context) ApplySpecific(contexts ...Context)

ApplySpecific takes a list of contexts and merges their contents into this one, keeping the keys specifically

func (Context) AsDeepKeyed

func (c Context) AsDeepKeyed() (out Context)

AsDeepKeyed returns a deep-key flattened version of this context

Examples:

Input   Context{"one": map[string]interface{}{"two": "deep"}}
Output  Context{".one.two": "deep"}

Input   Context{"one": Contexts{{"two": "deep"}}}
Output  Context{".one[0].two": "deep"}

func (Context) Bool

func (c Context) Bool(key string, def ...bool) bool

func (Context) Boolean

func (c Context) Boolean(key string) (value, ok bool)

func (Context) Bytes

func (c Context) Bytes(key string, def ...[]byte) []byte

Bytes returns the key's value as a byte slice, returning the given default if not found or not actually a byte slice value.

func (Context) CamelizeKeys

func (c Context) CamelizeKeys()

CamelizeKeys transforms all keys within the Context to be of CamelCased form

func (Context) Context

func (c Context) Context(key string) (ctx Context)

Context looks for the given key and if the value is of Context type, returns it

func (Context) Copy

func (c Context) Copy() (ctx Context)

Copy returns a deep-copy of this Context using values.DeepCopy

For problematic type cases, implementing the [deepcopy.Copyable] interface will enable the correct behaviour

func (Context) DeepKeys

func (c Context) DeepKeys() (keys []string)

DeepKeys returns a natural sorted list of .deep.keys representing the entire context

func (Context) DefaultStrings

func (c Context) DefaultStrings(key string, def ...[]string) []string

DefaultStrings is a wrapper around Strings() and returns the given default list of strings if the key is not found

func (Context) Delete

func (c Context) Delete(key string) (deleted bool)

Delete is a convenience wrapper around maps.DeleteKV

func (Context) DeleteKeys

func (c Context) DeleteKeys(keys ...string)

DeleteKeys is a batch wrapper around Delete()

func (Context) Empty

func (c Context) Empty() (empty bool)

Empty returns true if there is nothing stored in the Context

func (Context) FirstString

func (c Context) FirstString(key string) (value string, ok bool)

func (Context) Float64

func (c Context) Float64(key string, def ...float64) float64

func (Context) Get

func (c Context) Get(key string) (value interface{})

Get is a convenience wrapper around GetKV

func (Context) GetKV

func (c Context) GetKV(key string) (k string, v interface{})

GetKV is a convenience wrapper around maps.GetKV

func (Context) Has

func (c Context) Has(key string) (present bool)

Has returns true if the given Context key exists and is not nil

func (Context) HasExact

func (c Context) HasExact(key string) (present bool)

HasExact returns true if the specific Context key given exists and is not nil

func (Context) Int

func (c Context) Int(key string, def ...int) int

func (Context) Int64

func (c Context) Int64(key string, def ...int64) int64

func (Context) KebabKeys

func (c Context) KebabKeys()

KebabKeys transforms all keys within the Context to be of kebab-cased form

func (Context) Keys

func (c Context) Keys() (keys []string)

Keys is a convenience wrapper around maps.SortedKeys

func (Context) Len

func (c Context) Len() (count int)

Len returns the number of keys in the Context

func (Context) LowerCamelizeKeys

func (c Context) LowerCamelizeKeys()

LowerCamelizeKeys transforms all keys within the Context to be of lowerCamelCased form

func (Context) MatchQL

func (c Context) MatchQL(query string) (matched bool, err error)

MatchQL checks if the given context query statement matches this context

func (Context) PruneEmpty

func (c Context) PruneEmpty() (pruned Context)

func (Context) Select

func (c Context) Select(keys ...string) (selected map[string]interface{})

func (Context) SelectStringValues

func (c Context) SelectStringValues(keys ...string) (selected []string)

func (Context) SelectValues

func (c Context) SelectValues(keys ...string) (selected []interface{})

func (Context) Set

func (c Context) Set(key string, value interface{}) Context

Set CamelCases the given key and sets that within this Context

func (Context) SetKV

func (c Context) SetKV(key string, value interface{}) (err error)

SetKV is a convenience wrapper around maps.SetKV

func (Context) SetSpecific

func (c Context) SetSpecific(key string, value interface{}) Context

SetSpecific is like Set(), without CamelCasing the key

func (Context) Slice

func (c Context) Slice(key string) (list []interface{}, ok bool)

func (Context) String

func (c Context) String(key string, def ...string) string

String returns the key's value as a string, returning the given default if not found or not actually a string value.

func (Context) StringOrStrings

func (c Context) StringOrStrings(key string) (values []string)

StringOrStrings returns the key's value as a list of strings and if the key's actual value is not a list of strings, return that as a list of one string

func (Context) Strings

func (c Context) Strings(key string) (values []string)

Strings returns the key's value as a list of strings, returning an empty list if not found or not actually a list of strings

func (Context) Time

func (c Context) Time(key string, def ...time.Time) time.Time

func (Context) TimeDuration

func (c Context) TimeDuration(key string, def ...time.Duration) time.Duration

func (Context) ToEnviron

func (c Context) ToEnviron() (out []string)

ToEnviron returns this Context as a transformed []string slice where each key is converted to SCREAMING_SNAKE_CASE and the value is converted to a string (similarly to ToStringMap) and the key/value pair is concatenated into a single "K=V" string and appended to the output slice, sorted by key in natural order, suitable for use in os.Environ cases.

func (Context) ToJSON

func (c Context) ToJSON() (data []byte, err error)

ToJSON is a convenience wrapper around json.MarshalIndent, indented with two spaces and no prefix

func (Context) ToMap

func (c Context) ToMap() (out map[string]interface{})

ToMap returns a values.DeepCopy of this Context, transformed to a standard map[string]interface{} type, recursively (the output map has no references to the Context or Contexts types)

func (Context) ToStringMap

func (c Context) ToStringMap() (out map[string]string)

ToStringMap returns this Context as a transformed map[string]string structure, where each key's value is checked and if it's a string, use it as-is and if it's anything else, run it through fmt.Sprintf("%v") to make it a string

func (Context) ToTOML

func (c Context) ToTOML() (data []byte, err error)

ToTOML is a convenience wrapper around [toml.Marshal]

func (Context) ToYAML

func (c Context) ToYAML() (data []byte, err error)

ToYAML is a convenience wrapper around yaml.Marshal

func (Context) Uint

func (c Context) Uint(key string, def ...uint) uint

func (Context) Uint64

func (c Context) Uint64(key string, def ...uint64) uint64

func (Context) ValueAsInt

func (c Context) ValueAsInt(key string, def ...int) int

func (Context) ValueAsInt64

func (c Context) ValueAsInt64(key string, def ...int64) int64

type Contexts

type Contexts []Context

func (Contexts) FindQL

func (c Contexts) FindQL(query string) (found Contexts)

func (Contexts) FirstInt64Value

func (c Contexts) FirstInt64Value(key string) int64

func (Contexts) FirstIntValue

func (c Contexts) FirstIntValue(key string) int

func (Contexts) FirstStringValue

func (c Contexts) FirstStringValue(key string) string

func (Contexts) FirstValue

func (c Contexts) FirstValue(key string) interface{}

func (Contexts) Int64Values

func (c Contexts) Int64Values(key string) (values []int64)

func (Contexts) IntValues

func (c Contexts) IntValues(key string) (values []int)

func (Contexts) Len

func (c Contexts) Len() (count int)

func (Contexts) SelectStringValues

func (c Contexts) SelectStringValues(keys ...string) (values [][]string)

func (Contexts) SelectValues

func (c Contexts) SelectValues(keys ...string) (values [][]interface{})

func (Contexts) StringValues

func (c Contexts) StringValues(key string) (values []string)

func (Contexts) Values

func (c Contexts) Values(key string) (values []interface{})

Directories

Path Synopsis
Package cql provides a syntax parser for a "Context Query Language"
Package cql provides a syntax parser for a "Context Query Language"

Jump to

Keyboard shortcuts

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