mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Return Auth service Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Compose to run with SpiceDB and Auth svc Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth gRPC API Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove Users' policies Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Groups to internal Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use shared groups in Users Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove unused code Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use pkg Groups in Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove Things groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Make imports consistent Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Groups networking Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove things groups-specific API Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Things Clients to the root Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Clients to Users root Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Temporarily remove tracing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix imports Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add buffer config for gRPC Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth type for Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use Auth for login Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add temporary solution for refresh token Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Tokenizer interface Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Updade tokens issuing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix token issuing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update JWT validator and refactor Tokenizer Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Rename access timeout Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Rename login to authenticate Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Identify to use SubjectID Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add Auth to Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use the Auth service for Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth schema Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix Auth for Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add auth for addons (#14) Signed-off-by: Arvindh <arvindh91@gmail.com> Speparate Login and Refresh tokens Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Merge authN and authZ requests for things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add connect and disconnect Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update sharing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix policies addition and removal Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update relation with roels Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add gRPC to Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Assign and Unassign members to group and Listing of Group members (#15) * add auth for addons Signed-off-by: Arvindh <arvindh91@gmail.com> * add assign and unassign to group Signed-off-by: Arvindh <arvindh91@gmail.com> * add group incomplete repo implementation Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Move coap mqtt and ws policies to spicedb (#16) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Remove old policies Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> NOISSUE - Things authorize to return thingID (#18) This commit modifies the authorize endpoint to the grpc endpoint to return thingID. The authorize endpoint allows adapters to get the publisher of the message. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Add Groups to users service (#17) * add assign and unassign to group Signed-off-by: Arvindh <arvindh91@gmail.com> * add group incomplete repo implementation Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users stable 1 Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users stable 2 Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users & things Signed-off-by: Arvindh <arvindh91@gmail.com> * Amend signature Signed-off-by: Arvindh <arvindh91@gmail.com> * fix merge error Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Fix es code (#21) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Fix Bugs (#20) * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Test e2e (#19) * fix: connect method Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * fix: e2e Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * fix changes in sdk and e2e Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(docker): remove unnecessary port mapping Remove the port mapping for MQTT broker in the docker-compose.yml file. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Enable group listing Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(responses): update ChannelsPage struct The ChannelsPage struct in the responses.go file has been updated. The "Channels" field has been renamed to "Groups" to provide more accurate naming. This change ensures consistency and clarity in the codebase. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(things): add UpdateClientSecret method Add the UpdateClientSecret method to the things service. This method allows updating the client secret for a specific client identified by the provided token, id, and key parameters. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Use smaller buffers for gRPC Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Clean up tests (#22) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add Connect Disconnect endpoints (#23) * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: Things share with users (#25) * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> * add: things share with other users Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Rename gRPC Services (#24) * Rename things and users auth service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * docs: add authorization docs for gRPC services Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Rename things and users grpc services Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Remove mainflux.env package Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: Listing of things, channels, groups, users (#26) * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Users (#27) * feat(groups): rename redis package to events - Renamed the `redis` package to `events` in the `internal/groups` directory. - Updated the file paths and names accordingly. - This change reflects the more accurate purpose of the package and improves code organization. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(auth): Modify identity method Change request and response of identity method Add accessToken and refreshToken to Token response Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * clean up users, remove dead code Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(users): add unit tests for user service This commit adds unit tests for the user service in the `users` package. The tests cover various scenarios and ensure the correct behavior of the service. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: List of user groups & removed repeating code in groups (#29) * removed repeating code in list groups Signed-off-by: Arvindh <arvindh91@gmail.com> * add: list of user group Signed-off-by: Arvindh <arvindh91@gmail.com> * fix: otel handler operator name for endpoints Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Things Service (#28) * Rework things service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * add tests Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Auth Service (#30) * clean up auth service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(auth): remove unused import Remove the unused import of `emptypb` in `auth.pb.go`. This import is not being used in the codebase and can be safely removed. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Update API docs (#31) Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Remove TODO comments and cleanup the code Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Update dependenices Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Co-authored-by: b1ackd0t <28790446+rodneyosodo@users.noreply.github.com> Co-authored-by: Arvindh <30824765+arvindh123@users.noreply.github.com>
526 lines
14 KiB
Go
526 lines
14 KiB
Go
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package schema
|
|
|
|
import (
|
|
"encoding"
|
|
"errors"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
// NewDecoder returns a new Decoder.
|
|
func NewDecoder() *Decoder {
|
|
return &Decoder{cache: newCache()}
|
|
}
|
|
|
|
// Decoder decodes values from a map[string][]string to a struct.
|
|
type Decoder struct {
|
|
cache *cache
|
|
zeroEmpty bool
|
|
ignoreUnknownKeys bool
|
|
}
|
|
|
|
// AddAliasTag adds a tag used to locate custom field aliases.
|
|
// Defaults are "schema", "form" and "url".
|
|
func (d *Decoder) AddAliasTag(tag ...string) *Decoder {
|
|
d.cache.tags = append(d.cache.tags, tag...)
|
|
return d
|
|
}
|
|
|
|
// SetAliasTag overrides the tags.
|
|
func (d *Decoder) SetAliasTag(tag ...string) *Decoder {
|
|
d.cache.tags = tag
|
|
return d
|
|
}
|
|
|
|
// ZeroEmpty controls the behaviour when the decoder encounters empty values
|
|
// in a map.
|
|
// If z is true and a key in the map has the empty string as a value
|
|
// then the corresponding struct field is set to the zero value.
|
|
// If z is false then empty strings are ignored.
|
|
//
|
|
// The default value is false, that is empty values do not change
|
|
// the value of the struct field.
|
|
func (d *Decoder) ZeroEmpty(z bool) *Decoder {
|
|
d.zeroEmpty = z
|
|
return d
|
|
}
|
|
|
|
// IgnoreUnknownKeys controls the behaviour when the decoder encounters unknown
|
|
// keys in the map.
|
|
// If i is true and an unknown field is encountered, it is ignored. This is
|
|
// similar to how unknown keys are handled by encoding/json.
|
|
// If i is false then Decode will return an error. Note that any valid keys
|
|
// will still be decoded in to the target struct.
|
|
//
|
|
// To preserve backwards compatibility, the default value is false.
|
|
func (d *Decoder) IgnoreUnknownKeys(i bool) *Decoder {
|
|
d.ignoreUnknownKeys = i
|
|
return d
|
|
}
|
|
|
|
// RegisterConverter registers a converter function for a custom type.
|
|
func (d *Decoder) RegisterConverter(value interface{}, converterFunc Converter) *Decoder {
|
|
d.cache.registerConverter(value, converterFunc)
|
|
return d
|
|
}
|
|
|
|
// Decode decodes a map[string][]string to a struct.
|
|
//
|
|
// The first parameter must be a pointer to a struct.
|
|
//
|
|
// The second parameter is a map, typically url.Values from an HTTP request.
|
|
// Keys are "paths" in dotted notation to the struct fields and nested structs.
|
|
//
|
|
// See the package documentation for a full explanation of the mechanics.
|
|
func (d *Decoder) Decode(dst interface{}, src map[string][]string) error {
|
|
v := reflect.ValueOf(dst)
|
|
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
|
|
return errors.New("schema: interface must be a pointer to struct")
|
|
}
|
|
v = v.Elem()
|
|
t := v.Type()
|
|
errors := MultiError{}
|
|
for path, values := range src {
|
|
if parts, err := d.cache.parsePath(path, t); err == nil {
|
|
if err = d.decode(v, path, parts, values); err != nil {
|
|
errors[path] = err
|
|
}
|
|
} else if !d.ignoreUnknownKeys {
|
|
errors[path] = UnknownKeyError{Key: path}
|
|
}
|
|
}
|
|
errors.merge(d.checkRequired(t, src))
|
|
if len(errors) > 0 {
|
|
return errors
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// checkRequired checks whether required fields are empty
|
|
//
|
|
// check type t recursively if t has struct fields.
|
|
//
|
|
// src is the source map for decoding, we use it here to see if those required fields are included in src
|
|
func (d *Decoder) checkRequired(t reflect.Type, src map[string][]string) MultiError {
|
|
m, errs := d.findRequiredFields(t, "", "")
|
|
for key, fields := range m {
|
|
if isEmptyFields(fields, src) {
|
|
errs[key] = EmptyFieldError{Key: key}
|
|
}
|
|
}
|
|
return errs
|
|
}
|
|
|
|
// findRequiredFields recursively searches the struct type t for required fields.
|
|
//
|
|
// canonicalPrefix and searchPrefix are used to resolve full paths in dotted notation
|
|
// for nested struct fields. canonicalPrefix is a complete path which never omits
|
|
// any embedded struct fields. searchPrefix is a user-friendly path which may omit
|
|
// some embedded struct fields to point promoted fields.
|
|
func (d *Decoder) findRequiredFields(t reflect.Type, canonicalPrefix, searchPrefix string) (map[string][]fieldWithPrefix, MultiError) {
|
|
struc := d.cache.get(t)
|
|
if struc == nil {
|
|
// unexpect, cache.get never return nil
|
|
return nil, MultiError{canonicalPrefix + "*": errors.New("cache fail")}
|
|
}
|
|
|
|
m := map[string][]fieldWithPrefix{}
|
|
errs := MultiError{}
|
|
for _, f := range struc.fields {
|
|
if f.typ.Kind() == reflect.Struct {
|
|
fcprefix := canonicalPrefix + f.canonicalAlias + "."
|
|
for _, fspath := range f.paths(searchPrefix) {
|
|
fm, ferrs := d.findRequiredFields(f.typ, fcprefix, fspath+".")
|
|
for key, fields := range fm {
|
|
m[key] = append(m[key], fields...)
|
|
}
|
|
errs.merge(ferrs)
|
|
}
|
|
}
|
|
if f.isRequired {
|
|
key := canonicalPrefix + f.canonicalAlias
|
|
m[key] = append(m[key], fieldWithPrefix{
|
|
fieldInfo: f,
|
|
prefix: searchPrefix,
|
|
})
|
|
}
|
|
}
|
|
return m, errs
|
|
}
|
|
|
|
type fieldWithPrefix struct {
|
|
*fieldInfo
|
|
prefix string
|
|
}
|
|
|
|
// isEmptyFields returns true if all of specified fields are empty.
|
|
func isEmptyFields(fields []fieldWithPrefix, src map[string][]string) bool {
|
|
for _, f := range fields {
|
|
for _, path := range f.paths(f.prefix) {
|
|
if !isEmpty(f.typ, src[path]) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// isEmpty returns true if value is empty for specific type
|
|
func isEmpty(t reflect.Type, value []string) bool {
|
|
if len(value) == 0 {
|
|
return true
|
|
}
|
|
switch t.Kind() {
|
|
case boolType, float32Type, float64Type, intType, int8Type, int32Type, int64Type, stringType, uint8Type, uint16Type, uint32Type, uint64Type:
|
|
return len(value[0]) == 0
|
|
}
|
|
return false
|
|
}
|
|
|
|
// decode fills a struct field using a parsed path.
|
|
func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values []string) error {
|
|
// Get the field walking the struct fields by index.
|
|
for _, name := range parts[0].path {
|
|
if v.Type().Kind() == reflect.Ptr {
|
|
if v.IsNil() {
|
|
v.Set(reflect.New(v.Type().Elem()))
|
|
}
|
|
v = v.Elem()
|
|
}
|
|
|
|
// alloc embedded structs
|
|
if v.Type().Kind() == reflect.Struct {
|
|
for i := 0; i < v.NumField(); i++ {
|
|
field := v.Field(i)
|
|
if field.Type().Kind() == reflect.Ptr && field.IsNil() && v.Type().Field(i).Anonymous == true {
|
|
field.Set(reflect.New(field.Type().Elem()))
|
|
}
|
|
}
|
|
}
|
|
|
|
v = v.FieldByName(name)
|
|
}
|
|
// Don't even bother for unexported fields.
|
|
if !v.CanSet() {
|
|
return nil
|
|
}
|
|
|
|
// Dereference if needed.
|
|
t := v.Type()
|
|
if t.Kind() == reflect.Ptr {
|
|
t = t.Elem()
|
|
if v.IsNil() {
|
|
v.Set(reflect.New(t))
|
|
}
|
|
v = v.Elem()
|
|
}
|
|
|
|
// Slice of structs. Let's go recursive.
|
|
if len(parts) > 1 {
|
|
idx := parts[0].index
|
|
if v.IsNil() || v.Len() < idx+1 {
|
|
value := reflect.MakeSlice(t, idx+1, idx+1)
|
|
if v.Len() < idx+1 {
|
|
// Resize it.
|
|
reflect.Copy(value, v)
|
|
}
|
|
v.Set(value)
|
|
}
|
|
return d.decode(v.Index(idx), path, parts[1:], values)
|
|
}
|
|
|
|
// Get the converter early in case there is one for a slice type.
|
|
conv := d.cache.converter(t)
|
|
m := isTextUnmarshaler(v)
|
|
if conv == nil && t.Kind() == reflect.Slice && m.IsSliceElement {
|
|
var items []reflect.Value
|
|
elemT := t.Elem()
|
|
isPtrElem := elemT.Kind() == reflect.Ptr
|
|
if isPtrElem {
|
|
elemT = elemT.Elem()
|
|
}
|
|
|
|
// Try to get a converter for the element type.
|
|
conv := d.cache.converter(elemT)
|
|
if conv == nil {
|
|
conv = builtinConverters[elemT.Kind()]
|
|
if conv == nil {
|
|
// As we are not dealing with slice of structs here, we don't need to check if the type
|
|
// implements TextUnmarshaler interface
|
|
return fmt.Errorf("schema: converter not found for %v", elemT)
|
|
}
|
|
}
|
|
|
|
for key, value := range values {
|
|
if value == "" {
|
|
if d.zeroEmpty {
|
|
items = append(items, reflect.Zero(elemT))
|
|
}
|
|
} else if m.IsValid {
|
|
u := reflect.New(elemT)
|
|
if m.IsSliceElementPtr {
|
|
u = reflect.New(reflect.PtrTo(elemT).Elem())
|
|
}
|
|
if err := u.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(value)); err != nil {
|
|
return ConversionError{
|
|
Key: path,
|
|
Type: t,
|
|
Index: key,
|
|
Err: err,
|
|
}
|
|
}
|
|
if m.IsSliceElementPtr {
|
|
items = append(items, u.Elem().Addr())
|
|
} else if u.Kind() == reflect.Ptr {
|
|
items = append(items, u.Elem())
|
|
} else {
|
|
items = append(items, u)
|
|
}
|
|
} else if item := conv(value); item.IsValid() {
|
|
if isPtrElem {
|
|
ptr := reflect.New(elemT)
|
|
ptr.Elem().Set(item)
|
|
item = ptr
|
|
}
|
|
if item.Type() != elemT && !isPtrElem {
|
|
item = item.Convert(elemT)
|
|
}
|
|
items = append(items, item)
|
|
} else {
|
|
if strings.Contains(value, ",") {
|
|
values := strings.Split(value, ",")
|
|
for _, value := range values {
|
|
if value == "" {
|
|
if d.zeroEmpty {
|
|
items = append(items, reflect.Zero(elemT))
|
|
}
|
|
} else if item := conv(value); item.IsValid() {
|
|
if isPtrElem {
|
|
ptr := reflect.New(elemT)
|
|
ptr.Elem().Set(item)
|
|
item = ptr
|
|
}
|
|
if item.Type() != elemT && !isPtrElem {
|
|
item = item.Convert(elemT)
|
|
}
|
|
items = append(items, item)
|
|
} else {
|
|
return ConversionError{
|
|
Key: path,
|
|
Type: elemT,
|
|
Index: key,
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
return ConversionError{
|
|
Key: path,
|
|
Type: elemT,
|
|
Index: key,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
value := reflect.Append(reflect.MakeSlice(t, 0, 0), items...)
|
|
v.Set(value)
|
|
} else {
|
|
val := ""
|
|
// Use the last value provided if any values were provided
|
|
if len(values) > 0 {
|
|
val = values[len(values)-1]
|
|
}
|
|
|
|
if conv != nil {
|
|
if value := conv(val); value.IsValid() {
|
|
v.Set(value.Convert(t))
|
|
} else {
|
|
return ConversionError{
|
|
Key: path,
|
|
Type: t,
|
|
Index: -1,
|
|
}
|
|
}
|
|
} else if m.IsValid {
|
|
if m.IsPtr {
|
|
u := reflect.New(v.Type())
|
|
if err := u.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(val)); err != nil {
|
|
return ConversionError{
|
|
Key: path,
|
|
Type: t,
|
|
Index: -1,
|
|
Err: err,
|
|
}
|
|
}
|
|
v.Set(reflect.Indirect(u))
|
|
} else {
|
|
// If the value implements the encoding.TextUnmarshaler interface
|
|
// apply UnmarshalText as the converter
|
|
if err := m.Unmarshaler.UnmarshalText([]byte(val)); err != nil {
|
|
return ConversionError{
|
|
Key: path,
|
|
Type: t,
|
|
Index: -1,
|
|
Err: err,
|
|
}
|
|
}
|
|
}
|
|
} else if val == "" {
|
|
if d.zeroEmpty {
|
|
v.Set(reflect.Zero(t))
|
|
}
|
|
} else if conv := builtinConverters[t.Kind()]; conv != nil {
|
|
if value := conv(val); value.IsValid() {
|
|
v.Set(value.Convert(t))
|
|
} else {
|
|
return ConversionError{
|
|
Key: path,
|
|
Type: t,
|
|
Index: -1,
|
|
}
|
|
}
|
|
} else {
|
|
return fmt.Errorf("schema: converter not found for %v", t)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func isTextUnmarshaler(v reflect.Value) unmarshaler {
|
|
// Create a new unmarshaller instance
|
|
m := unmarshaler{}
|
|
if m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler); m.IsValid {
|
|
return m
|
|
}
|
|
// As the UnmarshalText function should be applied to the pointer of the
|
|
// type, we check that type to see if it implements the necessary
|
|
// method.
|
|
if m.Unmarshaler, m.IsValid = reflect.New(v.Type()).Interface().(encoding.TextUnmarshaler); m.IsValid {
|
|
m.IsPtr = true
|
|
return m
|
|
}
|
|
|
|
// if v is []T or *[]T create new T
|
|
t := v.Type()
|
|
if t.Kind() == reflect.Ptr {
|
|
t = t.Elem()
|
|
}
|
|
if t.Kind() == reflect.Slice {
|
|
// Check if the slice implements encoding.TextUnmarshaller
|
|
if m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler); m.IsValid {
|
|
return m
|
|
}
|
|
// If t is a pointer slice, check if its elements implement
|
|
// encoding.TextUnmarshaler
|
|
m.IsSliceElement = true
|
|
if t = t.Elem(); t.Kind() == reflect.Ptr {
|
|
t = reflect.PtrTo(t.Elem())
|
|
v = reflect.Zero(t)
|
|
m.IsSliceElementPtr = true
|
|
m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler)
|
|
return m
|
|
}
|
|
}
|
|
|
|
v = reflect.New(t)
|
|
m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler)
|
|
return m
|
|
}
|
|
|
|
// TextUnmarshaler helpers ----------------------------------------------------
|
|
// unmarshaller contains information about a TextUnmarshaler type
|
|
type unmarshaler struct {
|
|
Unmarshaler encoding.TextUnmarshaler
|
|
// IsValid indicates whether the resolved type indicated by the other
|
|
// flags implements the encoding.TextUnmarshaler interface.
|
|
IsValid bool
|
|
// IsPtr indicates that the resolved type is the pointer of the original
|
|
// type.
|
|
IsPtr bool
|
|
// IsSliceElement indicates that the resolved type is a slice element of
|
|
// the original type.
|
|
IsSliceElement bool
|
|
// IsSliceElementPtr indicates that the resolved type is a pointer to a
|
|
// slice element of the original type.
|
|
IsSliceElementPtr bool
|
|
}
|
|
|
|
// Errors ---------------------------------------------------------------------
|
|
|
|
// ConversionError stores information about a failed conversion.
|
|
type ConversionError struct {
|
|
Key string // key from the source map.
|
|
Type reflect.Type // expected type of elem
|
|
Index int // index for multi-value fields; -1 for single-value fields.
|
|
Err error // low-level error (when it exists)
|
|
}
|
|
|
|
func (e ConversionError) Error() string {
|
|
var output string
|
|
|
|
if e.Index < 0 {
|
|
output = fmt.Sprintf("schema: error converting value for %q", e.Key)
|
|
} else {
|
|
output = fmt.Sprintf("schema: error converting value for index %d of %q",
|
|
e.Index, e.Key)
|
|
}
|
|
|
|
if e.Err != nil {
|
|
output = fmt.Sprintf("%s. Details: %s", output, e.Err)
|
|
}
|
|
|
|
return output
|
|
}
|
|
|
|
// UnknownKeyError stores information about an unknown key in the source map.
|
|
type UnknownKeyError struct {
|
|
Key string // key from the source map.
|
|
}
|
|
|
|
func (e UnknownKeyError) Error() string {
|
|
return fmt.Sprintf("schema: invalid path %q", e.Key)
|
|
}
|
|
|
|
// EmptyFieldError stores information about an empty required field.
|
|
type EmptyFieldError struct {
|
|
Key string // required key in the source map.
|
|
}
|
|
|
|
func (e EmptyFieldError) Error() string {
|
|
return fmt.Sprintf("%v is empty", e.Key)
|
|
}
|
|
|
|
// MultiError stores multiple decoding errors.
|
|
//
|
|
// Borrowed from the App Engine SDK.
|
|
type MultiError map[string]error
|
|
|
|
func (e MultiError) Error() string {
|
|
s := ""
|
|
for _, err := range e {
|
|
s = err.Error()
|
|
break
|
|
}
|
|
switch len(e) {
|
|
case 0:
|
|
return "(0 errors)"
|
|
case 1:
|
|
return s
|
|
case 2:
|
|
return s + " (and 1 other error)"
|
|
}
|
|
return fmt.Sprintf("%s (and %d other errors)", s, len(e)-1)
|
|
}
|
|
|
|
func (e MultiError) merge(errors MultiError) {
|
|
for key, err := range errors {
|
|
if e[key] == nil {
|
|
e[key] = err
|
|
}
|
|
}
|
|
}
|