Skip to content

Conversation

@agermel
Copy link

@agermel agermel commented Dec 13, 2025

This PR implements a new feature for the config package to support generic value retrieval》

Generic Get Method (New Feature)
Currently, retrieving configuration values requires manual type conversion (e.g., c.Value("key").Int()). This PR adds a Get[T] helper function that leverages Go generics to directly return the desired typed value.

// Before
port, err := c.Value("server.port").Int()

// After
port, err := config.Get[int](c, "server.port")

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Dec 13, 2025
@agermel
Copy link
Author

agermel commented Dec 13, 2025

#3720 Solving this issue

@agermel
Copy link
Author

agermel commented Dec 13, 2025

Optimized Get[T] for primitive types by create a fast path that bypasses the overhead of Scan (unmarshall) for primitive types (bool, int, int64, float64, string)

	switch any(t).(type) {
	case bool:
		b, err := v.Bool()
		return any(b).(T), err
	case int64:
		i, err := v.Int()
		return any(i).(T), err
	case int:
		i, err := v.Int()
		return any(int(i)).(T), err
	case float64:
		f, err := v.Float()
		return any(f).(T), err
	case string:
		s, err := v.String()
		return any(s).(T), err
	}

shenqidebaozi
shenqidebaozi previously approved these changes Dec 25, 2025
@kratos-ci-bot
Copy link
Collaborator

⚠️ Non-English comments were found in the following locations:


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants