Skip to content

Commit

Permalink
Migrated changes for invalid name characters
Browse files Browse the repository at this point in the history
  • Loading branch information
kostasbalampekos committed Nov 2, 2023
1 parent f68870e commit 6f4516f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/mainflux/senml
module github.com/insighio/senml

go 1.13

Expand Down
27 changes: 21 additions & 6 deletions senml.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,25 @@ 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.
func Normalize(p Pack) (Pack, error) {
for _, r := range p.Records {
r.Name = replaceInvalidCharacters(r.Name)
}

// Validate ensures that all the BaseVersions are equal.
if err := Validate(p); err != nil {
return Pack{}, err
Expand All @@ -152,7 +167,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 @@ -242,10 +257,10 @@ func validateName(name string) error {
if (l == '-') || (l == ':') || (l == '.') || (l == '/') || (l == '_') {
return ErrBadChar
}
for _, l := range name {
if (l < 'a' || l > 'z') && (l < 'A' || l > 'Z') && (l < '0' || l > '9') && (l != '-') && (l != ':') && (l != '.') && (l != '/') && (l != '_') {
return ErrBadChar
}
}
// for _, l := range name {
// if (l < 'a' || l > 'z') && (l < 'A' || l > 'Z') && (l < '0' || l > '9') && (l != '-') && (l != ':') && (l != '.') && (l != '/') && (l != '_') {
// return ErrBadChar
// }
// }
return nil
}
2 changes: 1 addition & 1 deletion senml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"testing"

"github.com/mainflux/senml"
"github.com/insighio/senml"
"github.com/stretchr/testify/assert"
)

Expand Down

0 comments on commit 6f4516f

Please sign in to comment.