// LoadStruct takes a struct config, define flags based on it and parse the command line args.
331559
func (b *Backend) LoadStruct(ctx context.Context, cfg *confita.StructConfig) error {
	for _, field := range cfg.Fields {
		f := field

		if f.Backend != "" && f.Backend != b.Name() {
			continue
		}

		// Display all the flags and their default values but override the field only if the user has explicitely
		// set the flag.
		k := f.Value.Kind()
		switch {
		case f.Value.Type().String() == "time.Duration":
			// define the flag and its default value
			v := flag.Duration(f.Key, time.Duration(f.Default.Int()), "")
			// this function must be executed after the flag.Parse call.
			defer func() {
				// if the user has set the flag, save the value in the field.
				if isFlagSet(f.Key) {
					f.Value.SetInt(int64(*v))
				}
			}()
		case k == reflect.Bool:
			v := flag.Bool(f.Key, f.Default.Bool(), "")
			defer func() {
				if isFlagSet(f.Key) {
					f.Value.SetBool(*v)
				}
			}()
		case k >= reflect.Int && k <= reflect.Int64:
			v := flag.Int(f.Key, int(f.Default.Int()), "")
			defer func() {
				if isFlagSet(f.Key) {
					f.Value.SetInt(int64(*v))
				}
			}()
		case k >= reflect.Uint && k <= reflect.Uint64:
			v := flag.Uint(f.Key, uint(f.Default.Uint()), "")
			defer func() {
				if isFlagSet(f.Key) {
					f.Value.SetUint(uint64(*v))
				}
			}()
		case k >= reflect.Float32 && k <= reflect.Float64:
			v := flag.Float64(f.Key, f.Default.Float(), "")
			defer func() {
				if isFlagSet(f.Key) {
					f.Value.SetFloat(*v)
				}
			}()
		case k == reflect.String:
			v := flag.String(f.Key, f.Default.String(), "")
			defer func() {
				if isFlagSet(f.Key) {
					f.Value.SetString(*v)
				}
			}()
		default:
			flag.Var(&flagValue{f}, f.Key, "")
		}
	}

	flag.Parse()

	return nil
}
872534
func Struct(name string, opts ...FieldOpt) FieldOpt {
	return func(em *eventMetadata, ed *eventData) {
		em.writeStruct(name, uint8(len(opts)), 0)
		for _, opt := range opts {
			opt(em, ed)
		}
	}
}
833472
func (m *Module) LoadConfig(path string) error {
	_, err := os.Stat(path)
	if err != nil {
		if os.IsNotExist(err) {
			m.Byte = []byte(`{}`)
			return nil
		}
		return err
	}
	m.Byte, err = ioutil.ReadFile(path)
	if err != nil {
		return err
	}
	return nil
}
106348
func (e *Encoder) encodeStruct(valueField reflect.Value, typeField reflect.StructField, key string) error {
	e.currentRecursion++
	if e.currentRecursion > e.maxRecursion {
		return nil
	}

	e.parentKey += key
	err := e.encode(valueField)
	if err != nil {
		return err
	}

	// Do not reset the key if we are dealing with an embedded struct
	// as recursion level increases by one compared to normal cases
	if !typeField.Anonymous {
		e.parentKey = ""
	}

	e.currentRecursion--
	return nil
}
301518
func parseStruct(r reflect.Value, node *XMLNode, tag reflect.StructTag) error {
	t := r.Type()
	if r.Kind() == reflect.Ptr {
		if r.IsNil() { // create the structure if it's nil
			s := reflect.New(r.Type().Elem())
			r.Set(s)
			r = s
		}

		r = r.Elem()
		t = t.Elem()
	}

	// unwrap any payloads
	if payload := tag.Get("payload"); payload != "" {
		field, _ := t.FieldByName(payload)
		return parseStruct(r.FieldByName(payload), node, field.Tag)
	}

	for i := 0; i < t.NumField(); i++ {
		field := t.Field(i)
		if c := field.Name[0:1]; strings.ToLower(c) == c {
			continue // ignore unexported fields
		}

		// figure out what this field is called
		name := field.Name
		if field.Tag.Get("flattened") != "" && field.Tag.Get("locationNameList") != "" {
			name = field.Tag.Get("locationNameList")
		} else if locName := field.Tag.Get("locationName"); locName != "" {
			name = locName
		}

		// try to find the field by name in elements
		elems := node.Children[name]

		if elems == nil { // try to find the field in attributes
			if val, ok := node.findElem(name); ok {
				elems = []*XMLNode{{Text: val}}
			}
		}

		member := r.FieldByName(field.Name)
		for _, elem := range elems {
			err := parse(member, elem, field.Tag)
			if err != nil {
				return err
			}
		}
	}
	return nil
}
252771
func LoadJSON(raw json.RawMessage) (*Config, error) {
	obj, err := hcl.Parse(string(raw))
	if err != nil {
		return nil, fmt.Errorf(
			"Error parsing JSON document as HCL: %s", err)
	}

	// Start building the result
	hclConfig := &hclConfigurable{
		Root: obj,
	}

	return hclConfig.Config()
}
379453
func (e *EnvironmentLoader) Load(s interface{}) error {
	strct := structs.New(s)
	strctMap := strct.Map()
	prefix := e.getPrefix(strct)

	for key, val := range strctMap {
		field := strct.Field(key)

		if err := e.processField(prefix, field, key, val); err != nil {
			return err
		}
	}

	return nil
}
1277571
func (res *Resource) NewStruct() interface{} {
	if res.Value == nil {
		return nil
	}
	return reflect.New(utils.Indirect(reflect.ValueOf(res.Value)).Type()).Interface()
}
1135653
func newStructDef(apiDef *raml.APIDefinition, name, packageName, description string, properties map[string]interface{}) structDef {
	// generate struct's fields from type properties
	fields := make(map[string]fieldDef)
	for k, v := range properties {
		prop := raml.ToProperty(k, v)
		fields[prop.Name] = newFieldDef(apiDef, name, prop, packageName)
	}
	return structDef{
		Name:        strings.Title(commons.NormalizeIdentifier(name)),
		PackageName: packageName,
		Fields:      fields,
		Description: commons.ParseDescription(description),
	}
}
1373653
func LoadReader(fd io.Reader) (config Project, err error) {
	data, err := ioutil.ReadAll(fd)
	if err != nil {
		return config, err
	}
	err = yaml.UnmarshalStrict(data, &config)
	log.WithField("config", config).Debug("loaded config file")
	return config, err
}
422044
func (s *StructInjector) fillStruct() {
	if !s.Has {
		return
	}
	// if field is Static then set it to the value that passed by the caller,
	// so will have the static bindings already and we can just use that value instead
	// of creating new instance.
	destElem := IndirectValue(s.initRef)
	for _, f := range s.fields {
		// if field is Static then set it to the value that passed by the caller,
		// so will have the static bindings already and we can just use that value instead
		// of creating new instance.
		if f.Object.BindType == Static {
			destElem.FieldByIndex(f.FieldIndex).Set(f.Object.Value)
		}
	}
}
923675
func (cMgr *ConfigurationManager) handleStruct(rValue reflect.Value, tagName string) error {
	structType := rValue.Type()
	numOfField := structType.NumField()

	for i := 0; i < numOfField; i++ {
		structField := structType.Field(i)
		fieldValue := rValue.Field(i)
		keyName := cMgr.getKeyName(structField.Name, structField.Tag)
		if keyName == ignoreField {
			return nil
		}

		switch structField.Type.Kind() {
		case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
			reflect.Float32, reflect.Float64, reflect.Uint, reflect.Uint8, reflect.Uint16,
			reflect.Uint32, reflect.Uint64, reflect.Bool, reflect.Interface, reflect.Array,
			reflect.Slice:
			if fieldValue.CanSet() {
				err := cMgr.setValue(fieldValue, getTagKey(tagName, keyName))
				if err != nil {
					return err
				}
			}
		case reflect.Ptr:
			err := cMgr.handlePtr(fieldValue, getTagKey(tagName, keyName))
			if err != nil {
				return err
			}
		case reflect.Struct:
			err := cMgr.handleStruct(fieldValue, getTagKey(tagName, keyName))
			if err != nil {
				return err
			}
		case reflect.Map:
			err := cMgr.handleMap(rValue, fieldValue, getTagKey(tagName, keyName))
			if err != nil {
				return err
			}
		case reflect.Uintptr, reflect.Complex64, reflect.Complex128, reflect.Chan, reflect.Func,
			reflect.UnsafePointer:
			// ignore
		}
	}

	return nil
}
708658
func (w *Writer) WriteStruct(st interface{}) error {
	row, err := w.e.GetRow(st)
	if err != nil {
		return err
	}
	w.r++
	return w.w.Write(row)
}
442001
func parseStruct(s interface{}) (*gofiguration, error) {
	var v reflect.Value
	if reflect.TypeOf(s) != reflect.TypeOf(v) {
		v = reflect.ValueOf(s)

		if v.Kind() == reflect.PtrTo(reflect.TypeOf(s)).Kind() {
			v = reflect.Indirect(v)
		}
	} else {
		v = s.(reflect.Value)
	}

	if v.Kind() != reflect.Struct {
		return nil, ErrUnsupportedType
	}

	t := v.Type()

	gfg := &gofiguration{
		params: make(map[string]map[string]string),
		order:  DefaultOrder,
		fields: make(map[string]*gofiguritem),
		s:      s,
	}

	err := gfg.parseGofigureField(t)
	if err != nil {
		return nil, err
	}

	gfg.parseFields(v, t)

	return gfg, nil
}
117444
func (v *validate) validateStruct(ctx context.Context, parent reflect.Value, current reflect.Value, typ reflect.Type, ns []byte, structNs []byte, ct *cTag) {

	cs, ok := v.v.structCache.Get(typ)
	if !ok {
		cs = v.v.extractStructCache(current, typ.Name())
	}

	if len(ns) == 0 && len(cs.name) != 0 {

		ns = append(ns, cs.name...)
		ns = append(ns, '.')

		structNs = append(structNs, cs.name...)
		structNs = append(structNs, '.')
	}

	// ct is nil on top level struct, and structs as fields that have no tag info
	// so if nil or if not nil and the structonly tag isn't present
	if ct == nil || ct.typeof != typeStructOnly {

		var f *cField

		for i := 0; i < len(cs.fields); i++ {

			f = cs.fields[i]

			if v.isPartial {

				if v.ffn != nil {
					// used with StructFiltered
					if v.ffn(append(structNs, f.name...)) {
						continue
					}

				} else {
					// used with StructPartial & StructExcept
					_, ok = v.includeExclude[string(append(structNs, f.name...))]

					if (ok && v.hasExcludes) || (!ok && !v.hasExcludes) {
						continue
					}
				}
			}

			v.traverseField(ctx, parent, current.Field(f.idx), ns, structNs, f, f.cTags)
		}
	}

	// check if any struct level validations, after all field validations already checked.
	// first iteration will have no info about nostructlevel tag, and is checked prior to
	// calling the next iteration of validateStruct called from traverseField.
	if cs.fn != nil {

		v.slflParent = parent
		v.slCurrent = current
		v.ns = ns
		v.actualNs = structNs

		cs.fn(ctx, v)
	}
}
916487
func (k *Client) SendStruct(content interface{}) *Client {
	if marshalContent, err := json.Marshal(content); err != nil {
		k.Errors = append(k.Errors, err)
	} else {
		var val map[string]interface{}
		d := json.NewDecoder(bytes.NewBuffer(marshalContent))
		d.UseNumber()
		if err := d.Decode(&val); err != nil {
			k.Errors = append(k.Errors, err)
		} else {
			for key, v := range val {
				k.Data[key] = v
			}
		}
	}
	return k
}
607046
func getStructField(v interface{}, field string) (fieldVal interface{}, exists bool) {
	vv := reflect.ValueOf(v)
	if !vv.IsValid() {
		return nil, false
	}
	if vv.Kind() == reflect.Ptr {
		vv = vv.Elem()
	}
	fv := vv.FieldByName(field)
	if !fv.IsValid() {
		return nil, false
	}
	return fv.Interface(), true
}