-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added utf8 fix? * interface should be pointer to string * fix comments * check for UTF8 when decoding or encoding * remove wrapping * added tests * update changelog, prep for release
- Loading branch information
1 parent
d39a7ef
commit 840a19e
Showing
3 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Copyright 2022 Comcast Cable Communications Management, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package wrp | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"unicode/utf8" | ||
) | ||
|
||
var ( | ||
ErrNotUTF8 = errors.New("field contains non-utf-8 characters") | ||
ErrUnexpectedKind = errors.New("A struct or non-nil pointer to struct is required") | ||
) | ||
|
||
// UTF8 takes any struct verifies that it contains UTF-8 strings. | ||
func UTF8(v interface{}) error { | ||
value := reflect.ValueOf(v) | ||
if value.Kind() == reflect.Ptr && !value.IsNil() { | ||
value = value.Elem() | ||
} | ||
|
||
if value.Kind() != reflect.Struct { | ||
return fmt.Errorf("%w: %s", ErrUnexpectedKind, value.Kind()) | ||
} | ||
|
||
for i := 0; i < value.NumField(); i++ { | ||
ft := value.Type().Field(i) | ||
if len(ft.PkgPath) > 0 || ft.Anonymous { | ||
continue // skip embedded or unexported fields | ||
} | ||
|
||
f := value.Field(i) | ||
if !f.CanInterface() { | ||
continue // this should never happen, but ... you never know | ||
} | ||
|
||
if s, ok := f.Interface().(string); ok { | ||
if !utf8.ValidString(s) { | ||
return fmt.Errorf("%w: '%s:%v'", ErrNotUTF8, ft.Name, s) | ||
} | ||
fmt.Println(s) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters