// 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 (
	
	
	
	
	
)

// NewDecoder returns a new Decoder.
func () *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
}

// SetAliasTag changes the tag used to locate custom field aliases.
// The default tag is "schema".
func ( *Decoder) ( string) {
	.cache.tag = 
}

// 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 ( *Decoder) ( bool) {
	.zeroEmpty = 
}

// 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 ( *Decoder) ( bool) {
	.ignoreUnknownKeys = 
}

// RegisterConverter registers a converter function for a custom type.
func ( *Decoder) ( interface{},  Converter) {
	.cache.registerConverter(, )
}

// 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 ( *Decoder) ( interface{},  map[string][]string) error {
	 := reflect.ValueOf()
	if .Kind() != reflect.Ptr || .Elem().Kind() != reflect.Struct {
		return errors.New("schema: interface must be a pointer to struct")
	}
	 = .Elem()
	 := .Type()
	 := MultiError{}
	for ,  := range  {
		if ,  := .cache.parsePath(, );  == nil {
			if  = .decode(, , , );  != nil {
				[] = 
			}
		} else if !.ignoreUnknownKeys {
			[] = UnknownKeyError{Key: }
		}
	}
	.merge(.checkRequired(, ))
	if len() > 0 {
		return 
	}
	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 ( *Decoder) ( reflect.Type,  map[string][]string) MultiError {
	,  := .findRequiredFields(, "", "")
	for ,  := range  {
		if isEmptyFields(, ) {
			[] = EmptyFieldError{Key: }
		}
	}
	return 
}

// 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 ( *Decoder) ( reflect.Type, ,  string) (map[string][]fieldWithPrefix, MultiError) {
	 := .cache.get()
	if  == nil {
		// unexpect, cache.get never return nil
		return nil, MultiError{ + "*": errors.New("cache fail")}
	}

	 := map[string][]fieldWithPrefix{}
	 := MultiError{}
	for ,  := range .fields {
		if .typ.Kind() == reflect.Struct {
			 :=  + .canonicalAlias + "."
			for ,  := range .paths() {
				,  := .(.typ, , +".")
				for ,  := range  {
					[] = append([], ...)
				}
				.merge()
			}
		}
		if .isRequired {
			 :=  + .canonicalAlias
			[] = append([], fieldWithPrefix{
				fieldInfo: ,
				prefix:    ,
			})
		}
	}
	return , 
}

type fieldWithPrefix struct {
	*fieldInfo
	prefix string
}

// isEmptyFields returns true if all of specified fields are empty.
func isEmptyFields( []fieldWithPrefix,  map[string][]string) bool {
	for ,  := range  {
		for ,  := range .paths(.prefix) {
			,  := []
			if  && !isEmpty(.typ, ) {
				return false
			}
			for  := range  {
				// issue references:
				// https://github.com/gofiber/fiber/issues/1414
				// https://github.com/gorilla/schema/issues/176
				 := strings.IndexByte(, '.') != -1

				// for non required nested structs
				 := strings.HasSuffix(.prefix, ".") &&  == 

				// for required nested structs
				 := .prefix == "" &&  && strings.HasPrefix(, )

				// for non nested fields
				 := .prefix == "" && ! &&  == 
				if !isEmpty(.typ, []) && ( ||  || ) {
					return false
				}
			}
		}
	}
	return true
}

// isEmpty returns true if value is empty for specific type
func isEmpty( reflect.Type,  []string) bool {
	if len() == 0 {
		return true
	}
	switch .Kind() {
	case boolType, float32Type, float64Type, intType, int8Type, int32Type, int64Type, stringType, uint8Type, uint16Type, uint32Type, uint64Type:
		return len([0]) == 0
	}
	return false
}

// decode fills a struct field using a parsed path.
func ( *Decoder) ( reflect.Value,  string,  []pathPart,  []string) error {
	// Get the field walking the struct fields by index.
	for ,  := range [0].path {
		if .Type().Kind() == reflect.Ptr {
			if .IsNil() {
				.Set(reflect.New(.Type().Elem()))
			}
			 = .Elem()
		}

		// alloc embedded structs
		if .Type().Kind() == reflect.Struct {
			for  := 0;  < .NumField(); ++ {
				 := .Field()
				if .Type().Kind() == reflect.Ptr && .IsNil() && .Type().Field().Anonymous {
					.Set(reflect.New(.Type().Elem()))
				}
			}
		}

		 = .FieldByName()
	}
	// Don't even bother for unexported fields.
	if !.CanSet() {
		return nil
	}

	// Dereference if needed.
	 := .Type()
	if .Kind() == reflect.Ptr {
		 = .Elem()
		if .IsNil() {
			.Set(reflect.New())
		}
		 = .Elem()
	}

	// Slice of structs. Let's go recursive.
	if len() > 1 {
		 := [0].index
		if .IsNil() || .Len() < +1 {
			 := reflect.MakeSlice(, +1, +1)
			if .Len() < +1 {
				// Resize it.
				reflect.Copy(, )
			}
			.Set()
		}
		return .(.Index(), , [1:], )
	}

	// Get the converter early in case there is one for a slice type.
	 := .cache.converter()
	 := isTextUnmarshaler()
	if  == nil && .Kind() == reflect.Slice && .IsSliceElement {
		var  []reflect.Value
		 := .Elem()
		 := .Kind() == reflect.Ptr
		if  {
			 = .Elem()
		}

		// Try to get a converter for the element type.
		 := .cache.converter()
		if  == nil {
			 = builtinConverters[.Kind()]
			if  == 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", )
			}
		}

		for ,  := range  {
			if  == "" {
				if .zeroEmpty {
					 = append(, reflect.Zero())
				}
			} else if .IsValid {
				 := reflect.New()
				if .IsSliceElementPtr {
					 = reflect.New(reflect.PtrTo().Elem())
				}
				if  := .Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte());  != nil {
					return ConversionError{
						Key:   ,
						Type:  ,
						Index: ,
						Err:   ,
					}
				}
				if .IsSliceElementPtr {
					 = append(, .Elem().Addr())
				} else if .Kind() == reflect.Ptr {
					 = append(, .Elem())
				} else {
					 = append(, )
				}
			} else if  := (); .IsValid() {
				if  {
					 := reflect.New()
					.Elem().Set()
					 = 
				}
				if .Type() !=  && ! {
					 = .Convert()
				}
				 = append(, )
			} else {
				if strings.Contains(, ",") {
					 := strings.Split(, ",")
					for ,  := range  {
						if  == "" {
							if .zeroEmpty {
								 = append(, reflect.Zero())
							}
						} else if  := (); .IsValid() {
							if  {
								 := reflect.New()
								.Elem().Set()
								 = 
							}
							if .Type() !=  && ! {
								 = .Convert()
							}
							 = append(, )
						} else {
							return ConversionError{
								Key:   ,
								Type:  ,
								Index: ,
							}
						}
					}
				} else {
					return ConversionError{
						Key:   ,
						Type:  ,
						Index: ,
					}
				}
			}
		}
		 := reflect.Append(reflect.MakeSlice(, 0, 0), ...)
		.Set()
	} else {
		 := ""
		// Use the last value provided if any values were provided
		if len() > 0 {
			 = [len()-1]
		}

		if  != nil {
			if  := (); .IsValid() {
				.Set(.Convert())
			} else {
				return ConversionError{
					Key:   ,
					Type:  ,
					Index: -1,
				}
			}
		} else if .IsValid {
			if .IsPtr {
				 := reflect.New(.Type())
				if  := .Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte());  != nil {
					return ConversionError{
						Key:   ,
						Type:  ,
						Index: -1,
						Err:   ,
					}
				}
				.Set(reflect.Indirect())
			} else {
				// If the value implements the encoding.TextUnmarshaler interface
				// apply UnmarshalText as the converter
				if  := .Unmarshaler.UnmarshalText([]byte());  != nil {
					return ConversionError{
						Key:   ,
						Type:  ,
						Index: -1,
						Err:   ,
					}
				}
			}
		} else if  == "" {
			if .zeroEmpty {
				.Set(reflect.Zero())
			}
		} else if  := builtinConverters[.Kind()];  != nil {
			if  := (); .IsValid() {
				.Set(.Convert())
			} else {
				return ConversionError{
					Key:   ,
					Type:  ,
					Index: -1,
				}
			}
		} else {
			return fmt.Errorf("schema: converter not found for %v", )
		}
	}
	return nil
}

func isTextUnmarshaler( reflect.Value) unmarshaler {
	// Create a new unmarshaller instance
	 := unmarshaler{}
	if .Unmarshaler, .IsValid = .Interface().(encoding.TextUnmarshaler); .IsValid {
		return 
	}
	// 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 .Unmarshaler, .IsValid = reflect.New(.Type()).Interface().(encoding.TextUnmarshaler); .IsValid {
		.IsPtr = true
		return 
	}

	// if v is []T or *[]T create new T
	 := .Type()
	if .Kind() == reflect.Ptr {
		 = .Elem()
	}
	if .Kind() == reflect.Slice {
		// Check if the slice implements encoding.TextUnmarshaller
		if .Unmarshaler, .IsValid = .Interface().(encoding.TextUnmarshaler); .IsValid {
			return 
		}
		// If t is a pointer slice, check if its elements implement
		// encoding.TextUnmarshaler
		.IsSliceElement = true
		if  = .Elem(); .Kind() == reflect.Ptr {
			 = reflect.PtrTo(.Elem())
			 = reflect.Zero()
			.IsSliceElementPtr = true
			.Unmarshaler, .IsValid = .Interface().(encoding.TextUnmarshaler)
			return 
		}
	}

	 = reflect.New()
	.Unmarshaler, .IsValid = .Interface().(encoding.TextUnmarshaler)
	return 
}

// 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 ( ConversionError) () string {
	var  string

	if .Index < 0 {
		 = fmt.Sprintf("schema: error converting value for %q", .Key)
	} else {
		 = fmt.Sprintf("schema: error converting value for index %d of %q",
			.Index, .Key)
	}

	if .Err != nil {
		 = fmt.Sprintf("%s. Details: %s", , .Err)
	}

	return 
}

// UnknownKeyError stores information about an unknown key in the source map.
type UnknownKeyError struct {
	Key string // key from the source map.
}

func ( UnknownKeyError) () string {
	return fmt.Sprintf("schema: invalid path %q", .Key)
}

// EmptyFieldError stores information about an empty required field.
type EmptyFieldError struct {
	Key string // required key in the source map.
}

func ( EmptyFieldError) () string {
	return fmt.Sprintf("%v is empty", .Key)
}

// MultiError stores multiple decoding errors.
//
// Borrowed from the App Engine SDK.
type MultiError map[string]error

func ( MultiError) () string {
	 := ""
	for ,  := range  {
		 = .Error()
		break
	}
	switch len() {
	case 0:
		return "(0 errors)"
	case 1:
		return 
	case 2:
		return  + " (and 1 other error)"
	}
	return fmt.Sprintf("%s (and %d other errors)", , len()-1)
}

func ( MultiError) ( MultiError) {
	for ,  := range  {
		if [] == nil {
			[] = 
		}
	}
}