func()

in metric/system/hwmon/hwmon_types.go [95:143]


func (sm *SensorMetrics) Fold(v structform.ExtVisitor) error {
	val := reflect.ValueOf(sm).Elem()
	types := reflect.TypeOf(sm).Elem()

	err := v.OnObjectStart(val.NumField(), structform.AnyType)
	if err != nil {
		return fmt.Errorf("error starting object in Fold: %w", err)
	}

	for i := 0; i < val.NumField(); i++ {
		if val.Field(i).CanInterface() {

			// Fetch the struct tags
			iface := val.Field(i).Interface()
			skey, tagExists := types.Field(i).Tag.Lookup("struct")
			if !tagExists {
				skey = types.Field(i).Name
			} else {
				skey = strings.Split(skey, ",")[0]
			}

			if skey == "value" {
				skey = sm.sensorType.fileKey
			}

			// Cast to an opt type, then create a nested dict
			castUint, ok := iface.(opt.Uint)
			if ok && castUint.Exists() {
				err := v.OnKey(skey)
				if err != nil {
					return fmt.Errorf("error in OnKey for %s: %w", skey, err)
				}
				// This "inserts" the unit of the metric into the dict as an extra key
				err = v.OnUint64Object(map[string]uint64{sm.sensorType.units: castUint.ValueOr(0)})
				if err != nil {
					return fmt.Errorf("error in OnKey for %s: %w", skey, err)
				}

			}
		}

	}
	err = v.OnObjectFinished()
	if err != nil {
		return fmt.Errorf("error in OnObjectFinished for %s: %w", sm.Label, err)
	}

	return nil
}