Documentation
¶
Overview ¶
JSONX scanner Copyright (C) Philip Schlump, 2014-2017
JSONX scanner Copyright (C) Philip Schlump, 2014-2017
package SetStruct
Index ¶
- Variables
- func AppendError(meta map[string]MetaInfo, metaName, msg string)
- func AppendErrorSetBy(meta map[string]MetaInfo, metaName, msg string)
- func AssignParseTreeToData(f interface{}, meta map[string]MetaInfo, from *JsonToken, ...) (err error)
- func CheckGfNamesValid(tag string) (rv bool, badTag string)
- func ErrorSummary(format string, f interface{}, meta map[string]MetaInfo) (msg string, err error)
- func Exists(name string) bool
- func FindOptPrefix(prefix string, jNopt []string) (rv string, found bool)
- func Fopen(fn string, mode string) (file *os.File, err error)
- func GenArrayPath(path, name string, idx int) (rv string)
- func GenStructPath(path, name string) (rv string)
- func GetCurrentWorkingDirectory() string
- func GetFilenames(dir string, rec bool) (filenames, dirs []string)
- func GetHostName() string
- func GetTopTag(tag, tagName string) (rv string)
- func GetUsedCap(alloc string) (used, cap int)
- func GetVv(xName, path string, meta map[string]MetaInfo, topTag string) (req bool, typ_s, minV_s, maxV_s, minLen_s, maxLen_s, list_s, valRe_s string, ...)
- func InArray(lookFor string, inArr []string) bool
- func IntMax(a, b int) int
- func IntMin(a, b int) int
- func InterfaceSlice(slice interface{}) []interface{}
- func MatchName(jNname, colName, jsonXattrName string) bool
- func OptIsFound(opt []string) bool
- func OptNoTypeError(opt []string) bool
- func OptOmitEmpty(opt []string) bool
- func OptSetField(jNname, name string, from *JsonToken, jNopt []string, val reflect.Value, ...)
- func OptString(opt []string) bool
- func OptTypeOf(opt []string) bool
- func OptUnused(opt []string) bool
- func ParseGfJsonX(jN string) (name string, opt []string)
- func ParseLineIntoWords(line string) []string
- func PrintJsonToken(data []JsonToken)
- func PrintTokenSlice(data []JsonToken, depth int)
- func ProcessPath(js *JsonXScanner, fn string) (outFn []string, found bool)
- func SVar(v interface{}) string
- func SVarI(v interface{}) string
- func SearchArrayTooMany(jNname, colName string, from *JsonToken, maxPos int) (nSupp int, ok bool)
- func SearchBool(jNname, colName string, from *JsonToken) (dv bool, ok bool, fn string, ln int)
- func SearchBoolTop(jNname, colName string, from *JsonToken) (dv bool, ok bool, fn string, ln int)
- func SearchFloat(jNname, colName string, from *JsonToken) (dv float64, ok bool, fn string, ln int)
- func SearchFloatTop(jNname, colName string, from *JsonToken) (dv float64, ok bool, fn string, ln int)
- func SearchInt(jNname, colName string, from *JsonToken) (dv int64, ok bool, fn string, ln int)
- func SearchIntTop(jNname, colName string, from *JsonToken) (dv int64, ok bool, fn string, ln int)
- func SearchString(jNname, colName string, from *JsonToken) (dv string, ok bool, fn string, ln int)
- func SearchStringTop(jNname, colName string, from *JsonToken) (dv string, ok bool, fn string, ln int)
- func SetDataSource(meta map[string]MetaInfo, metaName string, ds SetByType)
- func SetDataSourceFnLn(meta map[string]MetaInfo, metaName string, ds SetByType, fn string, ln int)
- func SetDebugFlag(name string, onOff bool)
- func SetDebugFlags(flags string)
- func SetDefaults(f interface{}, meta map[string]MetaInfo, xName, topTag, path string) (err error)
- func StringsAfter(s, sep string) string
- func StringsBefore(s, sep string) string
- func StringsDeQuote(str string) (rv string)
- func TokenToErrorMsg(data *JsonToken) (rv []string)
- func Unmarshal(Src string, In []byte, Out interface{}) (meta map[string]MetaInfo, err error)
- func UnmarshalFile(fn string, Out interface{}) (meta map[string]MetaInfo, err error)
- func UnmarshalString(Src, In string, Out interface{}) (meta map[string]MetaInfo, err error)
- func ValidateRequired(f interface{}, meta map[string]MetaInfo) (err error)
- func ValidateValues(f interface{}, meta map[string]MetaInfo, xName, topTag, path string) (err error)
- type ChildType
- type ErrorItem
- type FileSource
- type JsonScannerInputStack
- type JsonToken
- func NameValueNode(name JsonToken, ptr JsonToken) (rv JsonToken)
- func NameValueNodeNullError(name JsonToken) (rv JsonToken)
- func ParseJsonX(buf []byte, js *JsonXScanner) (rv *JsonToken, NErrors int)
- func SearchArray(jNname, colName string, from *JsonToken, arrPos int) (dv *JsonToken, ok bool)
- func SearchHashLength(jNname, colName string, from *JsonToken) (nSupp int, fromHash *JsonToken)
- func SearchHashValue(from *JsonToken, ii int) (ok bool, key string, rawValue *JsonToken)
- func SearchStruct(jNname, colName string, from *JsonToken) (dv *JsonToken, ok bool)
- type JsonXConfig
- func (jx *JsonXConfig) AssignParseTreeToData(f interface{}, meta map[string]MetaInfo, from *JsonToken, ...) (err error)
- func (jx *JsonXConfig) GetDv(xName, path string, meta map[string]MetaInfo, topTag string) (dv, name, metaName string)
- func (sd *JsonXConfig) SetDefaults(f interface{}, meta map[string]MetaInfo, xName, topTag, path string) (err error)
- func (jx *JsonXConfig) SetFirstInWins(b bool) *JsonXConfig
- func (jx *JsonXConfig) SetTopName(s string) *JsonXConfig
- func (vr *JsonXConfig) ValidateRequired(f interface{}, meta map[string]MetaInfo) (err error)
- func (vl *JsonXConfig) ValidateValues(f interface{}, meta map[string]MetaInfo, xName, topTag, path string) (err error)
- type JsonXFunc
- type JsonXInput
- type JsonXScanner
- func (js *JsonXScanner) MapFunction(name string, impl JsonXFunc)
- func (js *JsonXScanner) PrevToken(offset int) (t TokenType)
- func (js *JsonXScanner) ScanBytes(buf []byte)
- func (js *JsonXScanner) ScanFile(fn string)
- func (js *JsonXScanner) ScanInput(fn string, source JsonXInput)
- func (js *JsonXScanner) ScanString(buf string)
- func (js *JsonXScanner) SetCommentsNest(ns bool) *JsonXScanner
- func (js *JsonXScanner) SetOptions(opt ScanOptions) *JsonXScanner
- func (js *JsonXScanner) SetStartEndMarker(sm, em string) *JsonXScanner
- type MetaInfo
- type ParserType
- func (pt *ParserType) AddError(code int, msg string, js *JsonXScanner, offset int)
- func (pt *ParserType) AddErrorGA(code int, msg string, js *JsonXScanner, offset int, ga string)
- func (pt *ParserType) AdvTok()
- func (pt *ParserType) AdvTok2()
- func (pt *ParserType) ConvertValueToId(name JsonToken) (rv JsonToken)
- func (pt *ParserType) IsA(js *JsonXScanner, nth int, tt TokenType) bool
- func (pt *ParserType) IsAny(js *JsonXScanner, nth int) bool
- func (pt *ParserType) IsCloseArray(js *JsonXScanner, nth int) bool
- func (pt *ParserType) IsCloseHash(js *JsonXScanner, nth int) bool
- func (pt *ParserType) IsId(js *JsonXScanner, nth int) bool
- func (pt *ParserType) IsOpenArray(js *JsonXScanner, nth int) bool
- func (pt *ParserType) IsOpenHash(js *JsonXScanner, nth int) bool
- func (pt *ParserType) IsUnknown(js *JsonXScanner, nth int) bool
- func (pt *ParserType) IsValue(js *JsonXScanner, nth int) bool
- func (pt *ParserType) ValueCanConvertToId(js *JsonXScanner, nth int) bool
- type PostCreate
- type PreCreate
- type PullFromFunc
- type ScanOptions
- type SetByType
- type TokenType
- type TypeValidator
- type UnmarshalType
- type Unmarshaler
Constants ¶
This section is empty.
Variables ¶
var Db map[string]bool
var DebugFlagPrintTypeLookedUp bool
var ErrDefaultsNotSet = errors.New("Defaults were not set")
var ErrDuplicateExtra = errors.New("More than one field was marked as 'extra'. Only 1 allowed.")
var ErrFound = errors.New("Error Found")
var ErrMissingRequiredValues = errors.New("Missing Required Values")
var ErrNotSet = errors.New("Value was not set, not a settable field.")
var PerTypeValidator map[string]TypeValidator
var PostCreateMap map[string]PostCreate
var PreCreateMap map[string]PreCreate
var ValidGfNames = []string{
"gfDefault",
"gfDefaultEnv",
"gfDefaultFromKey",
"gfIgnore",
"gfAlloc",
"gfType",
"gfMinValue",
"gfMaxValue",
"gfMinLen",
"gfMaxLen",
"gfListValue",
"gfValidRE",
"gfIgnoreDefault",
"gfRequired",
"gfTag",
"gfNoSet",
"gfJsonX",
"gfPrompt",
"gfPromptPassword",
}
Functions ¶
func AppendError ¶
func AppendErrorSetBy ¶
func AssignParseTreeToData ¶
func CheckGfNamesValid ¶
func ErrorSummary ¶
ErrorSummary Returns a summary of any errors in meta - formatted for output. If no errors then err will be nil, else ErrFound == h"Error Found" will be returned.
format == "text" Format into text for a log file or output to the screen. format == "color" Format for output in color to a screen format == "json" Format in JSON for output to a log expecing a JSON object.
TODO test
func FindOptPrefix ¶
FindOptPrefix looks in the array of parsed options for an tag staring with "prefix". If it is found then found is true and the stuff after prefix is returned. Example: "setField:" is the prefix, then an option of "setField:MyName" will return true and "MyName".
func GenArrayPath ¶
newPath := genArrayPath(path, name, ii)
func GenStructPath ¶
newPath := genPath ( path, "struct", name )
func GetCurrentWorkingDirectory ¶
func GetCurrentWorkingDirectory() string
func GetFilenames ¶
func GetHostName ¶
func GetHostName() string
func GetUsedCap ¶
func InterfaceSlice ¶
func InterfaceSlice(slice interface{}) []interface{}
TODO test InterfaceSlice converts from a interface that refers to a slize to an array of interfaces with each one refering to one element in the slice. An example of using it is:
ss := InterfaceSlice(f.Interface()) for ii, vv := range ss { fmt.Printf("at %d %v\n", ii, vv) }
From: http://stackoverflow.com/questions/12753805/type-converting-slices-of-interfaces-in-go
func MatchName ¶
jNname The name specified from the tag. '*' - means use column name match. colName The name of the data column in the structure jsonXattrName The name of the attribugte in the JSON dictionary
func OptIsFound ¶
func OptNoTypeError ¶
func OptOmitEmpty ¶
func OptSetField ¶
func ParseGfJsonX ¶
jNname, jNalt := ParseGfJsonX(jN) "Name,options..." Options are:
is-found true/false if data value is found no-type-error ignore type errors to int<-string results in no error (allows for multiple type values) type-of return the "type" of the RValue *,unused take all the unused values and assign to this one. *,omitempty On Marshall - if field is NULL then omit it.
func ParseLineIntoWords ¶
func PrintJsonToken ¶
func PrintJsonToken(data []JsonToken)
func PrintTokenSlice ¶
func ProcessPath ¶
func ProcessPath(js *JsonXScanner, fn string) (outFn []string, found bool)
func SVar ¶
func SVar(v interface{}) string
SVar convert a variable to it's JSON representation and return
func SVarI ¶
func SVarI(v interface{}) string
SVarI convert a variable to it's JSON representation with indendted JSON
func SearchArrayTooMany ¶
NChild := SearchArrayNChild(jNname, name, from) if nSupp, ok := SearchArrayTooMany(jName, name, from, f.Len()); !ok {
func SearchBool ¶
func SearchBoolTop ¶
dvb, ok, fn, ln := SearchBoolTop(jNname, name, from)
func SearchFloat ¶
func SearchFloatTop ¶
func SearchIntTop ¶
func SearchStringTop ¶
func SetDataSourceFnLn ¶
func SetDebugFlag ¶
func SetDebugFlags ¶
func SetDebugFlags(flags string)
func SetDefaults ¶
func StringsAfter ¶
func StringsBefore ¶
func StringsDeQuote ¶
func TokenToErrorMsg ¶
TokenToErrorMsg returns an array of strings that contains any error messages from the scan/parse process. These are the errors in the syntax tree.
func UnmarshalFile ¶
func UnmarshalString ¶
func ValidateRequired ¶
Types ¶
type ChildType ¶
type ChildType int
func DeriveArrayType ¶
godebug.Db2Printf(db120, "%s Could be of type %s, %s%s\n", MiscLib.ColorYellow, DeriveArrayType(from), godebug.LF(), MiscLib.ColorReset)
func DeriveObjectType ¶
godebug.Db2Printf(db120, "%s Could be of type %s, %s%s\n", MiscLib.ColorYellow, DeriveObjectType(from), godebug.LF(), MiscLib.ColorReset)
type FileSource ¶
type FileSource struct { }
func NewFileSource ¶
func NewFileSource() *FileSource
func (*FileSource) Exists ¶
func (fs *FileSource) Exists(fn string) (rv bool)
Exists returns true if the speified name, 'fn', exists and is an appropriate type of object to open and read from.
type JsonScannerInputStack ¶
type JsonToken ¶
type JsonToken struct { Number int TokenNo TokenType TokenNoValue TokenType Name string Value string NValue int FValue float64 IValue int64 BValue bool LineNo int ColPos int FileName string ErrorMsg string Children []JsonToken GeneratedAt string UsedData bool // used in assignment, true if this value has been used. AssignedTo []string // List of who this value was assigned to. }
func NameValueNode ¶
func NameValueNodeNullError ¶
func ParseJsonX ¶
func ParseJsonX(buf []byte, js *JsonXScanner) (rv *JsonToken, NErrors int)
func SearchArray ¶
newFrom, ok := SearchArray(jNname, name, from, ii) // ok is false if array-subscript, 'ii' is out of range
func SearchHashLength ¶
NInHash := SearchHashLength(jNname, name, from)
func SearchHashValue ¶
keyString, rawValue := SearchHashValue(jNname, name, from, ii) // - # of children in TokenObjectStart > 0, if so then allocate SearchHashLength must be run first to pick out 'from' from the original data. // func SearchHashValue(jNname, colName string, from *JsonToken, ii int) (ok bool, key string, rawValue *JsonToken) {
type JsonXConfig ¶
type JsonXConfig struct { FirstInWins bool `gfDefault:"false"` // used by this file TopName string `gfDefault:""` // used by this file - the name for things that are not in a struct. OutputLineWidth int `gfDefault:"120"` // max number of chars, 0 indicates output should go down page. OutputInJSON bool `gfDefault:"false"` // InputPath []string `gfDefault:"['./']"` // Path to use for searching for files for __include__, __require__ }
This single config
func NewJsonX ¶
func NewJsonX() (rv *JsonXConfig)
func (*JsonXConfig) AssignParseTreeToData ¶
func (jx *JsonXConfig) AssignParseTreeToData(f interface{}, meta map[string]MetaInfo, from *JsonToken, xName, topTag, path string) (err error)
Assign to 'f' the data from 'from'. Keep an ongoing 'path' as we make recursive calls. 'meta' is data from setting defauls on the top level - overwrite data in it as we assing data to stuff in 'f'.
func (*JsonXConfig) SetDefaults ¶
func (sd *JsonXConfig) SetDefaults(f interface{}, meta map[string]MetaInfo, xName, topTag, path string) (err error)
SetDefaults uses a set of tags to set default values and allocate storage inside a structure.
func (*JsonXConfig) SetFirstInWins ¶
func (jx *JsonXConfig) SetFirstInWins(b bool) *JsonXConfig
chainable
func (*JsonXConfig) SetTopName ¶
func (jx *JsonXConfig) SetTopName(s string) *JsonXConfig
chainable -- name
func (*JsonXConfig) ValidateRequired ¶
func (vr *JsonXConfig) ValidateRequired(f interface{}, meta map[string]MetaInfo) (err error)
ValidateRequired is a final check - after other processing that required values are set.
func (*JsonXConfig) ValidateValues ¶
func (vl *JsonXConfig) ValidateValues(f interface{}, meta map[string]MetaInfo, xName, topTag, path string) (err error)
ValidateValues uses tags on the structures to validate the values that are in the structure. Special tags are allowed for non-validation of default values thereby allowing for defauts that are not valid data.
type JsonXFunc ¶
type JsonXFunc func(js *JsonXScanner, args []string) (rv string)
type JsonXInput ¶
type JsonXScanner ¶
type JsonXScanner struct { State int // Pos int // where in buf StateSt []byte // LineNo int // Current Line number ColPos int // Current Col Pos FileName string // Current File Name PrevLineNo int // Prev Line number PrevColPos int // Prev Col Pos PrevFileName string // Prev File Name Toks []JsonToken // Options ScanOptions // InputSource JsonXInput // the input interface, defaults to file system Buf []byte // the input text in memory Funcs map[string]JsonXFunc // functions to process Data map[string]interface{} // EmitNo int // PathTop string // Current directory PosSaved bool SavedLineNo int // Current Line number SavedColPos int // Current Col Pos // contains filtered or unexported fields }
func NewScan ¶
func NewScan(fn string) (rv *JsonXScanner)
func (*JsonXScanner) MapFunction ¶
func (js *JsonXScanner) MapFunction(name string, impl JsonXFunc)
func (*JsonXScanner) PrevToken ¶
func (js *JsonXScanner) PrevToken(offset int) (t TokenType)
if PrevToken(-2) == TokenId && PrevToken(-1) != TokenColon {
func (*JsonXScanner) ScanBytes ¶
func (js *JsonXScanner) ScanBytes(buf []byte)
func (*JsonXScanner) ScanFile ¶
func (js *JsonXScanner) ScanFile(fn string)
func (*JsonXScanner) ScanInput ¶
func (js *JsonXScanner) ScanInput(fn string, source JsonXInput)
func (*JsonXScanner) ScanString ¶
func (js *JsonXScanner) ScanString(buf string)
func (*JsonXScanner) SetCommentsNest ¶
func (js *JsonXScanner) SetCommentsNest(ns bool) *JsonXScanner
js.SetCommentsNest(tf)
func (*JsonXScanner) SetOptions ¶
func (js *JsonXScanner) SetOptions(opt ScanOptions) *JsonXScanner
func (*JsonXScanner) SetStartEndMarker ¶
func (js *JsonXScanner) SetStartEndMarker(sm, em string) *JsonXScanner
type MetaInfo ¶
type MetaInfo struct { LineNo int // if > 0 then used in formatting of error messages. FileName string // if != "" then used in formatting of error messages. Can be any string. ErrorMsg []string // Error messages SetBy SetByType // Who set the value the last DataFrom SetByType // Source of the data value // contains filtered or unexported fields }
type ParserType ¶
func (*ParserType) AddError ¶
func (pt *ParserType) AddError(code int, msg string, js *JsonXScanner, offset int)
func (*ParserType) AddErrorGA ¶
func (pt *ParserType) AddErrorGA(code int, msg string, js *JsonXScanner, offset int, ga string)
func (*ParserType) AdvTok ¶
func (pt *ParserType) AdvTok()
func (*ParserType) AdvTok2 ¶
func (pt *ParserType) AdvTok2()
func (*ParserType) ConvertValueToId ¶
func (pt *ParserType) ConvertValueToId(name JsonToken) (rv JsonToken)
func (*ParserType) IsA ¶
func (pt *ParserType) IsA(js *JsonXScanner, nth int, tt TokenType) bool
func (*ParserType) IsAny ¶
func (pt *ParserType) IsAny(js *JsonXScanner, nth int) bool
func (*ParserType) IsCloseArray ¶
func (pt *ParserType) IsCloseArray(js *JsonXScanner, nth int) bool
func (*ParserType) IsCloseHash ¶
func (pt *ParserType) IsCloseHash(js *JsonXScanner, nth int) bool
func (*ParserType) IsId ¶
func (pt *ParserType) IsId(js *JsonXScanner, nth int) bool
func (*ParserType) IsOpenArray ¶
func (pt *ParserType) IsOpenArray(js *JsonXScanner, nth int) bool
func (*ParserType) IsOpenHash ¶
func (pt *ParserType) IsOpenHash(js *JsonXScanner, nth int) bool
func (*ParserType) IsUnknown ¶
func (pt *ParserType) IsUnknown(js *JsonXScanner, nth int) bool
func (*ParserType) IsValue ¶
func (pt *ParserType) IsValue(js *JsonXScanner, nth int) bool
func (*ParserType) ValueCanConvertToId ¶
func (pt *ParserType) ValueCanConvertToId(js *JsonXScanner, nth int) bool
} else if pt.IsValue(js, 0) && ValueCanConvertToId(js, 0) {
type PostCreate ¶
type PostCreate func(in interface{}) (err error) // Called after validation(both) -- Allows for post validation processing
type PullFromFunc ¶
var PullFromDefault PullFromFunc
This function can be a closure that pulls data from Redis, Etcd or some other external source.
type ScanOptions ¶
type TokenType ¶
type TokenType int
const ( TokenUnknown TokenType = 0 // Hm... TokenObject TokenType = 1 // on '}' TokenArray TokenType = 2 // on ']' TokenString TokenType = 3 // A value TokenId TokenType = 4 // "name" or name before a ":" TokenNull TokenType = 7 // A value TokenFloat TokenType = 8 // A value TokenBool TokenType = 9 // A value TokenInt TokenType = 10 // A value TokenObjectStart TokenType = 11 // on '{' TokenArrayStart TokenType = 12 // on '[' TokenObjectEnd TokenType = 13 // on '{' TokenArrayEnd TokenType = 14 // on '[' TokenColon TokenType = 15 // on ':' TokenComma TokenType = 16 // on ',' TokenNameValue TokenType = 17 // name:value in a hash )
type TypeValidator ¶
type UnmarshalType ¶
type UnmarshalType int
const ( UTString UnmarshalType = 1 UTInt UnmarshalType = 2 UTFloat UnmarshalType = 3 UTBool UnmarshalType = 4 UTDictionary UnmarshalType = 5 UTArray UnmarshalType = 6 )