OpenNHP/opennhp · error

unsupported type: %T

Error message

unsupported type: %T

What it means

UpdateTomlConfig performs regex-based replacement of a `key = "..."` line in a TOML file, but its type switch only supports string values. Any non-string value (int, bool, float, slice) hits the default branch and returns this error naming the actual Go type (%T). It is a capability limitation of this utility, not a file problem.

Solutions

  1. Pass the value as a string, e.g. strconv.Itoa(port) or strconv.FormatBool(enabled), since only string is supported.
  2. Extend UpdateTomlConfig with cases for other types (int, bool, float64) or use a real TOML library such as github.com/pelletier/go-toml/v2 to set the value.
  3. Note the regex only matches quoted values anyway (`".+"`), so unquoted TOML values must be handled separately even for string-typed rewrites of numeric keys.

Example fix

// before
err := utils.UpdateTomlConfig("etc/config.toml", "port", 8080) // unsupported type: int
// after
err := utils.UpdateTomlConfig("etc/config.toml", "port", strconv.Itoa(8080))
Defensive patterns

Strategy: type-guard

Validate before calling

if s, ok := value.(string); !ok {
    return fmt.Errorf("UpdateTomlConfig only accepts string values; got %T", value)
}

Type guard

func isStringValue(v any) (string, bool) {
    s, ok := v.(string)
    return s, ok
}

Try / catch

if err := utils.UpdateTomlConfig(path, key, value); err != nil && strings.HasPrefix(err.Error(), "unsupported type:") {
    // fall back to go-toml v2 tree.Set(key, value) and write back
}

Prevention

When it happens

Trigger: Calling UpdateTomlConfig(path, key, 42), (path, key, true), or any non-string value - e.g. RotateTeeKey/RotateAgentKey style code passing a numeric port or boolean flag to update in a TOML config.

Common situations: Trying to update `port = 8080` or `enabled = true` entries in config.toml; refactored code where the value type changed from string to int after a config schema change; passing values read back from JSON unmarshalling, where numbers arrive as float64.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of OpenNHP/opennhp@6e04ca5ff0 (2026-09-07). Data as JSON: /api/errors/521ce3eb98cc99e8. Report an issue: GitHub.

Appendix: source

Thrown at nhp/utils/utils.go:165

	}

	return data, nil
}

func UpdateTomlConfig(filePath string, key string, value any) error {
	content, err := os.ReadFile(filePath)
	if err != nil {
		return err
	}

	var newContent string

	switch value := value.(type) {
	case string:
		re := regexp.MustCompile(`(?m)^\s*` + key + `\s*=\s*".+"\s*$`)
		newContent = re.ReplaceAllString(string(content), fmt.Sprintf("%s = \"%s\"", key, value))
	default:
		return fmt.Errorf("unsupported type: %T", value)
	}

	err = os.WriteFile(filePath, []byte(newContent), 0644) //nolint:gosec // G306: Config files are typically world-readable
	if err != nil {
		return err
	}

	return nil
}

View on GitHub (pinned to 6e04ca5ff0)