Home / Function/ SetValWithStruct() — fiber Function Reference

SetValWithStruct() — fiber Function Reference

Architecture documentation for the SetValWithStruct() function in request.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  037f4a74_18dc_ed85_7e7a_85569b6174c7["SetValWithStruct()"]
  57f8b776_0c63_5c88_3581_d70765402a0c["request.go"]
  037f4a74_18dc_ed85_7e7a_85569b6174c7 -->|defined in| 57f8b776_0c63_5c88_3581_d70765402a0c
  style 037f4a74_18dc_ed85_7e7a_85569b6174c7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

client/request.go lines 1066–1122

func SetValWithStruct(p WithStruct, tagName string, v any) {
	valueOfV := reflect.ValueOf(v)
	typeOfV := reflect.TypeOf(v)

	// The value should be a struct or a pointer to a struct.
	if typeOfV.Kind() == reflect.Pointer && typeOfV.Elem().Kind() == reflect.Struct {
		valueOfV = valueOfV.Elem()
		typeOfV = typeOfV.Elem()
	} else if typeOfV.Kind() != reflect.Struct {
		return
	}

	// A helper function to set values.
	var setVal func(name string, val reflect.Value)
	setVal = func(name string, val reflect.Value) {
		switch val.Kind() {
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			p.Add(name, strconv.Itoa(int(val.Int())))
		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
			p.Add(name, strconv.FormatUint(val.Uint(), 10))
		case reflect.Float32, reflect.Float64:
			p.Add(name, strconv.FormatFloat(val.Float(), 'f', -1, 64))
		case reflect.Complex64, reflect.Complex128:
			p.Add(name, strconv.FormatComplex(val.Complex(), 'f', -1, 128))
		case reflect.Bool:
			if val.Bool() {
				p.Add(name, "true")
			} else {
				p.Add(name, "false")
			}
		case reflect.String:
			p.Add(name, val.String())
		case reflect.Slice, reflect.Array:
			for i := 0; i < val.Len(); i++ {
				setVal(name, val.Index(i))
			}
		default:
			return
		}
	}

	for i := 0; i < typeOfV.NumField(); i++ {
		field := typeOfV.Field(i)
		if !field.IsExported() {
			continue
		}

		name := field.Tag.Get(tagName)
		if name == "" {
			name = field.Name
		}
		val := valueOfV.Field(i)
		// To cover slice and array, we delete the val then add it.
		p.Del(name)
		setVal(name, val)
	}
}

Domain

Subdomains

Defined In

Frequently Asked Questions

What does SetValWithStruct() do?
SetValWithStruct() is a function in the fiber codebase, defined in client/request.go.
Where is SetValWithStruct() defined?
SetValWithStruct() is defined in client/request.go at line 1066.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free