Skip to content

Commit

Permalink
Update senml.go, fixed character replacing
Browse files Browse the repository at this point in the history
  • Loading branch information
ftylitak authored Nov 1, 2023
1 parent 91de157 commit 7fa6652
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions senml.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ func Encode(p Pack, format Format) ([]byte, error) {
}
}

func replaceInvalidCharacters(name string) string {
out := []rune(name)
for pos, l := range name {
if (l < 'a' || l > 'z') && (l < 'A' || l > 'Z') && (l < '0' || l > '9') && (l != '-') && (l != ':') && (l != '.') && (l != '/') && (l != '_') {
out[pos] = '_'
}
}

return string(out)
}

// Normalize removes all the base values and expands records values with the base items.
// The base fields apply to the entries in the Record and also to all Records after
// it up to, but not including, the next Record that has that same base field.
Expand Down Expand Up @@ -152,7 +163,7 @@ func Normalize(p Pack) (Pack, error) {
if len(r.BaseName) > 0 {
bname = r.BaseName
}
r.Name = bname + r.Name
r.Name = replaceInvalidCharacters(bname + r.Name)
r.Time = btime + r.Time
if r.Sum != nil {
*r.Sum = bsum + *r.Sum
Expand Down Expand Up @@ -182,17 +193,6 @@ func Normalize(p Pack) (Pack, error) {
return p, nil
}

func replaceInvalidCharacters(name string) string {
out := []rune(name)
for pos, l := range name {
if (l < 'a' || l > 'z') && (l < 'A' || l > 'Z') && (l < '0' || l > '9') && (l != '-') && (l != ':') && (l != '.') && (l != '/') && (l != '_') {
out[pos] = '_'
}
}

return string(out)
}

// Validate validates SenML records.
func Validate(p Pack) error {
var bver uint
Expand All @@ -219,8 +219,6 @@ func Validate(p Pack) error {
if len(name) == 0 {
return ErrEmptyName
}

r.Name = replaceInvalidCharacters(name)

var valCnt int
if r.Value != nil {
Expand Down

0 comments on commit 7fa6652

Please sign in to comment.