expanded logging in mapping.go
This commit is contained in:
@@ -2,8 +2,7 @@ package data
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -48,8 +47,12 @@ type Mapping struct {
|
|||||||
// Values returns the values contained in a Mapping as MappingValues.
|
// Values returns the values contained in a Mapping as MappingValues.
|
||||||
func (mapping Mapping) Values() MappingValues {
|
func (mapping Mapping) Values() MappingValues {
|
||||||
if mapping.vals == nil {
|
if mapping.vals == nil {
|
||||||
|
log.Debug("Mapping values are nil, returning empty MappingValues")
|
||||||
return MappingValues{}
|
return MappingValues{}
|
||||||
}
|
}
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"values_count": len(*mapping.vals),
|
||||||
|
}).Debug("Retrieved Mapping values")
|
||||||
return *mapping.vals
|
return *mapping.vals
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,30 +77,40 @@ func (mapping *Mapping) Data() []byte {
|
|||||||
|
|
||||||
// HasDuplicateKeys returns true if two keys in a mapping are identical.
|
// HasDuplicateKeys returns true if two keys in a mapping are identical.
|
||||||
func (mapping *Mapping) HasDuplicateKeys() bool {
|
func (mapping *Mapping) HasDuplicateKeys() bool {
|
||||||
|
log.Debug("Checking for duplicate keys in Mapping")
|
||||||
seen_values := make(map[string]bool)
|
seen_values := make(map[string]bool)
|
||||||
values := mapping.Values()
|
values := mapping.Values()
|
||||||
for _, pair := range values {
|
for _, pair := range values {
|
||||||
key, _ := pair[0].Data()
|
key, _ := pair[0].Data()
|
||||||
if _, present := seen_values[key]; present {
|
if _, present := seen_values[key]; present {
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"duplicate_key": key,
|
||||||
|
}).Warn("Found duplicate key in Mapping")
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
seen_values[key] = true
|
seen_values[key] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Debug("No duplicate keys found in Mapping")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// GoMapToMapping converts a Go map of unformatted strings to *Mapping.
|
// GoMapToMapping converts a Go map of unformatted strings to *Mapping.
|
||||||
func GoMapToMapping(gomap map[string]string) (mapping *Mapping, err error) {
|
func GoMapToMapping(gomap map[string]string) (mapping *Mapping, err error) {
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"input_map_size": len(gomap),
|
||||||
|
}).Debug("Converting Go map to Mapping")
|
||||||
map_vals := MappingValues{}
|
map_vals := MappingValues{}
|
||||||
for k, v := range gomap {
|
for k, v := range gomap {
|
||||||
key_str, kerr := ToI2PString(k)
|
key_str, kerr := ToI2PString(k)
|
||||||
if kerr != nil {
|
if kerr != nil {
|
||||||
|
log.WithError(kerr).Error("Failed to convert key to I2PString")
|
||||||
err = kerr
|
err = kerr
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val_str, verr := ToI2PString(v)
|
val_str, verr := ToI2PString(v)
|
||||||
if verr != nil {
|
if verr != nil {
|
||||||
|
log.WithError(verr).Error("Failed to convert value to I2PString")
|
||||||
err = verr
|
err = verr
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -107,27 +120,46 @@ func GoMapToMapping(gomap map[string]string) (mapping *Mapping, err error) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
mapping = ValuesToMapping(map_vals)
|
mapping = ValuesToMapping(map_vals)
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"mapping_size": len(map_vals),
|
||||||
|
}).Debug("Successfully converted Go map to Mapping")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the string parsing error indicates that the Mapping
|
// Check if the string parsing error indicates that the Mapping
|
||||||
// should no longer be parsed.
|
// should no longer be parsed.
|
||||||
func stopValueRead(err error) bool {
|
func stopValueRead(err error) bool {
|
||||||
return err.Error() == "error parsing string: zero length"
|
result := err.Error() == "error parsing string: zero length"
|
||||||
|
if result {
|
||||||
|
log.WithError(err).Debug("Stopping value read due to zero length error")
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine if the first byte in a slice of bytes is the provided byte.
|
// Determine if the first byte in a slice of bytes is the provided byte.
|
||||||
func beginsWith(bytes []byte, chr byte) bool {
|
func beginsWith(bytes []byte, chr byte) bool {
|
||||||
return len(bytes) != 0 &&
|
/*
|
||||||
bytes[0] == chr
|
return len(bytes) != 0 &&
|
||||||
|
bytes[0] == chr
|
||||||
|
*/
|
||||||
|
result := len(bytes) != 0 && bytes[0] == chr
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"bytes_length": len(bytes),
|
||||||
|
"expected_char": string(chr),
|
||||||
|
"result": result,
|
||||||
|
}).Debug("Checked if bytes begin with specific character")
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadMapping returns Mapping from a []byte.
|
// ReadMapping returns Mapping from a []byte.
|
||||||
// The remaining bytes after the specified length are also returned.
|
// The remaining bytes after the specified length are also returned.
|
||||||
// Returns a list of errors that occurred during parsing.
|
// Returns a list of errors that occurred during parsing.
|
||||||
func ReadMapping(bytes []byte) (mapping Mapping, remainder []byte, err []error) {
|
func ReadMapping(bytes []byte) (mapping Mapping, remainder []byte, err []error) {
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"input_length": len(bytes),
|
||||||
|
}).Debug("Reading Mapping from bytes")
|
||||||
if len(bytes) < 3 {
|
if len(bytes) < 3 {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(logrus.Fields{
|
||||||
"at": "ReadMapping",
|
"at": "ReadMapping",
|
||||||
"reason": "zero length",
|
"reason": "zero length",
|
||||||
}).Warn("mapping format violation")
|
}).Warn("mapping format violation")
|
||||||
@@ -137,16 +169,18 @@ func ReadMapping(bytes []byte) (mapping Mapping, remainder []byte, err []error)
|
|||||||
}
|
}
|
||||||
size, remainder, e := NewInteger(bytes, 2)
|
size, remainder, e := NewInteger(bytes, 2)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
|
log.WithError(e).Error("Failed to read Mapping size")
|
||||||
err = append(err, e)
|
err = append(err, e)
|
||||||
}
|
}
|
||||||
if size.Int() == 0 {
|
if size.Int() == 0 {
|
||||||
|
log.Warn("Mapping size is zero")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mapping.size = size
|
mapping.size = size
|
||||||
map_bytes := remainder[:mapping.size.Int()]
|
map_bytes := remainder[:mapping.size.Int()]
|
||||||
remainder = remainder[mapping.size.Int():]
|
remainder = remainder[mapping.size.Int():]
|
||||||
if len(remainder) == 0 {
|
if len(remainder) == 0 {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(logrus.Fields{
|
||||||
"at": "ReadMapping",
|
"at": "ReadMapping",
|
||||||
"reason": "zero length",
|
"reason": "zero length",
|
||||||
}).Warn("mapping format violation")
|
}).Warn("mapping format violation")
|
||||||
@@ -161,20 +195,38 @@ func ReadMapping(bytes []byte) (mapping Mapping, remainder []byte, err []error)
|
|||||||
err = append(err, mappingValueErrs...)
|
err = append(err, mappingValueErrs...)
|
||||||
mapping.vals = vals
|
mapping.vals = vals
|
||||||
if len(mappingValueErrs) > 0 {
|
if len(mappingValueErrs) > 0 {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(logrus.Fields{
|
||||||
"at": "ReadMapping",
|
"at": "ReadMapping",
|
||||||
"reason": "error parsing mapping values",
|
"reason": "error parsing mapping values",
|
||||||
}).Warn("mapping format violation")
|
}).Warn("mapping format violation")
|
||||||
e := errors.New("error parsing mapping values")
|
e := errors.New("error parsing mapping values")
|
||||||
err = append(err, e)
|
err = append(err, e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"mapping_size": mapping.size.Int(),
|
||||||
|
"values_count": len(*mapping.vals),
|
||||||
|
"remainder_length": len(remainder),
|
||||||
|
"error_count": len(err),
|
||||||
|
}).Debug("Finished reading Mapping")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMapping creates a new *Mapping from []byte using ReadMapping.
|
// NewMapping creates a new *Mapping from []byte using ReadMapping.
|
||||||
// Returns a pointer to Mapping unlike ReadMapping.
|
// Returns a pointer to Mapping unlike ReadMapping.
|
||||||
func NewMapping(bytes []byte) (values *Mapping, remainder []byte, err []error) {
|
func NewMapping(bytes []byte) (values *Mapping, remainder []byte, err []error) {
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"input_length": len(bytes),
|
||||||
|
}).Debug("Creating new Mapping")
|
||||||
|
|
||||||
objvalues, remainder, err := ReadMapping(bytes)
|
objvalues, remainder, err := ReadMapping(bytes)
|
||||||
values = &objvalues
|
values = &objvalues
|
||||||
|
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"values_count": len(values.Values()),
|
||||||
|
"remainder_length": len(remainder),
|
||||||
|
"error_count": len(err),
|
||||||
|
}).Debug("Finished creating new Mapping")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user