Documentation
¶
Overview ¶
Package jsontype provides functionality for getting types inside of a json file
Index ¶
- Variables
- func ComparePaths(path1, path2 []string) string
- func DiagnoseFieldInfo(field *FieldInfo, name string)
- func FieldInfoToString(field *FieldInfo, indent string) string
- func IsContainerType(t DetectedType) bool
- func IsDelim(token json.Token, expectDelim json.Delim) bool
- func IsMixedContainer(field *FieldInfo) bool
- func MergerToString(m *Merger, indent string, isLast bool) string
- func PathToString(path []string) string
- func PlanToString(plan *MergePlan, indent string, isLast bool) string
- func PrintMergerTree(m *Merger, prefix string, w io.Writer)
- func SnapshotMerger(name string, m *Merger) string
- func SnapshotPlan(name string, field *FieldInfo, logger *slog.Logger) string
- func StringToPath(s string) []string
- func TypesToString(types []DetectedType) []string
- type ArrayStrategy
- type Base64Encoding
- type Base64Variant
- type DefaultStream
- type DetectedType
- type FieldInfo
- type MergePlan
- type Merger
- type ObjectMergeStrategy
- type PlanKind
- type Stream
Constants ¶
This section is empty.
Variables ¶
var Base64Variants = []Base64Variant{ { Type: TypeBase64Std, Regexp: reBase64Std, Encoding: base64.StdEncoding, }, { Type: TypeBase64URL, Regexp: reBase64URL, Encoding: base64.URLEncoding, }, { Type: TypeBase64RawStd, Regexp: reBase64RawStd, Encoding: base64.RawStdEncoding, }, { Type: TypeBase64RawURL, Regexp: reBase64RawURL, Encoding: base64.RawURLEncoding, }, }
Functions ¶
func ComparePaths ¶
ComparePaths shows the difference between two paths
func DiagnoseFieldInfo ¶
DiagnoseFieldInfo prints detailed diagnostic info about a FieldInfo tree
func FieldInfoToString ¶
FieldInfoToString converts a FieldInfo tree to a readable string
func IsContainerType ¶
func IsContainerType(t DetectedType) bool
func IsMixedContainer ¶
IsMixedContainer returns true if container has different types of children elements
func MergerToString ¶
MergerToString converts a Merger to a human-readable tree
func PathToString ¶
func PlanToString ¶
PlanToString converts a MergePlan to a human-readable ASCII tree This makes future regressions scream immediately
func SnapshotMerger ¶
SnapshotMerger creates a golden test snapshot of a merger
func SnapshotPlan ¶
SnapshotPlan creates a golden test snapshot of a plan
func StringToPath ¶
func TypesToString ¶
func TypesToString(types []DetectedType) []string
Types ¶
type ArrayStrategy ¶
type ArrayStrategy int
const ( ArrayCollapse ArrayStrategy = iota // use "" ArrayKeepIndices // use "0", "1", ... )
type Base64Encoding ¶ added in v0.1.1
type Base64Variant ¶ added in v0.1.1
type Base64Variant struct {
Type DetectedType
Regexp *regexp.Regexp
Encoding Base64Encoding
}
type DefaultStream ¶
func NewJSONStream ¶
func NewJSONStream(r io.Reader) *DefaultStream
func (*DefaultStream) SkipValue ¶
func (s *DefaultStream) SkipValue() error
type DetectedType ¶
type DetectedType string
const ( TypeUnknown DetectedType = "unknown" TypeNull DetectedType = "null" TypeString DetectedType = "string" TypeBool DetectedType = "bool" TypeInt32 DetectedType = "int32" TypeInt64 DetectedType = "int64" TypeFloat64 DetectedType = "float64" // Containers TypeObj DetectedType = "object" TypeObjInt DetectedType = "object_int" TypeArray DetectedType = "array" )
const ( // Common TypeUUID DetectedType = "string-uuid" // f3a9c2e7-6b4d-4f81-9a6c-2d8e5b71c0fa TypeFilepathWindows DetectedType = "string-filepath-windows" // C:/user/file TypeEmail DetectedType = "string-email" // [email protected] TypePhone DetectedType = "string-phone" // +380661153394 // Web TypeLink DetectedType = "string-link" // https://google.com or google.com/search TypeDomain DetectedType = "string-domain" // google.com // Encoding TypeHEX DetectedType = "string-hex" // "a9f3c2e7b4d81f6a" TypeBase64Std DetectedType = "string-b64-std" // wqFIb2xhL+S4lueVjCtHbyE= TypeBase64URL DetectedType = "string-b64-url" // wqFIb2xhL-S4lueVjCtHbyE= TypeBase64RawStd DetectedType = "string-b64-raw-std" // wqFIb2xhL+S4lueVjCtHbyE TypeBase64RawURL DetectedType = "string-b64-raw-url" // wqFIb2xhL-S4lueVjCtHbyE // Networking TypeIPv4 DetectedType = "string-ipv4" // 127.0.0.1 TypeIPv4WithMask DetectedType = "string-ipv4-with-mask" // 127.0.0.1/32 TypeIPv6 DetectedType = "string-ipv6" // 2a03:2880:21ff:001f:face:b00c:dead:beef TypeIPv4PortPair DetectedType = "string-ipv4-port-pair" // 127.0.0.1:443 TypeIPv6PortPair DetectedType = "string-ipv6-port-pair" // [2a03:2880:21ff:1f:face:b00c:dead:beef]:43792 TypeMAC DetectedType = "string-mac" // 9e:3b:74:a1:5f:c2 )
Extended string detection/analysis Detection code is at ./detect_str_type.go
func DetectBase64 ¶ added in v0.1.1
func DetectBase64(s string) (DetectedType, bool)
func DetectHex ¶ added in v0.1.1
func DetectHex(s string) (DetectedType, bool)
func DetectStrType ¶ added in v0.0.5
func DetectStrType(s string) DetectedType
DetectStrType detects the type of a string value
func GetChilderTypes ¶
func GetChilderTypes(field *FieldInfo) (types []DetectedType)
func InferMergedContainerType ¶
func InferMergedContainerType(children []*FieldInfo, key string) DetectedType
type FieldInfo ¶
type FieldInfo struct {
Parent *FieldInfo
// Full path like ["obj1", "obj2", "field"]
Path []string
// If container -- will contain one of the following types: TypeObj, TypeObjInt, TypeObjArray
Type DetectedType
// Container-specific info
Children []*FieldInfo // Ordered children for objects/arrays
// Key: "0", "1", "2" for arrays; "1", "5", "12" for obj_int
ChildrenMap map[string]*FieldInfo // Quick lookup by name/key
}
FieldInfo represents a single field/path in the JSON structure It serves only for a run through a single file (since)
type MergePlan ¶
type MergePlan struct {
Kind PlanKind
// For arrays
ArrayStrategy ArrayStrategy
Elem *MergePlan
// For objects
Fields map[string]*MergePlan
}
MergePlan describes the shape of the merged result
type Merger ¶
type Merger struct {
// full path of this node (immutable after creation)
Path []string
// map( label : set(type) )
LabeledTypesMap map[string]map[DetectedType]struct{}
// how much times each type was met
TypesMap map[DetectedType]struct{}
// children keyed by the immediate child key (for arrays use "0", "1", etc as keys).
// if type isn't mixed (for some labels) -- all data is written under the same key ""
ChildrenMap map[string]*Merger
// tracks the order in which children were added
ChildrenKeys []string
}
Merger represents aggregated information for a single JSON path. Path is the full path (slice of keys). Children map contains sub-path nodes.
func ExecuteMerge ¶
func ExecuteMerge( plan *MergePlan, label string, fields []*FieldInfo, logger *slog.Logger, ) *Merger
ExecuteMerge executes a merge plan on the given field infos parentPath is the path we're building (with wildcards for collapsed arrays) fields are the FieldInfos at this level
func MergeFieldInfo ¶
MergeFieldInfo is the main entry point - creates plan and executes it
func (*Merger) AddTypes ¶
func (m *Merger) AddTypes(label string, types ...DetectedType)
type ObjectMergeStrategy ¶
type ObjectMergeStrategy struct {
// contains filtered or unexported fields
}
func NewObjectMergeStrategy ¶
func NewObjectMergeStrategy(logger *slog.Logger) *ObjectMergeStrategy
type Stream ¶
type Stream interface {
// NextToken returns next token from a json stream
// A Token holds a value of one of these types:
//
// - [Delim], for the four JSON delimiters [ ] { }
// - bool, for JSON booleans
// - float64, for JSON numbers
// - [Number], for JSON numbers
// - string, for JSON string literals
// - nil, for JSON null
//
// At the end of the stream returns EOF
Token() (json.Token, error)
More() bool
SkipValue() error
}
