Skip to content

Commit

Permalink
Simplify some platform converter code
Browse files Browse the repository at this point in the history
  • Loading branch information
futugyou committed Jan 9, 2025
1 parent c9f8756 commit ead2078
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
12 changes: 4 additions & 8 deletions extensions/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ func GetMapKeys[Key comparable, Value any](m map[Key]Value) []Key {
}

// wo golang verison is 1.20, can not use slices lib.
func MapToSlice[T any, K comparable](m map[K]T, transform func(T) any) []any {
result := make([]any, 0, len(m))
func MapToSliceDirect[T any, K comparable](m map[K]T) []T {
result := make([]T, 0, len(m))
for _, v := range m {
if transform != nil {
result = append(result, transform(v))
} else {
result = append(result, v)
}
result = append(result, v)
}
return result
}

func MapToSlice2[K comparable, V any, R any](m map[K]V, transform func(K, V) R) []R {
func MapToSlice[K comparable, V any, R any](m map[K]V, transform func(K, V) R) []R {
result := make([]R, 0, len(m))
for k, v := range m {
if transform != nil {
Expand Down
36 changes: 14 additions & 22 deletions infr-project/application/platform_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,14 @@ func (s *PlatformService) convertToPlatformModelWebhooks(hooks []platform.Webhoo
}

func (s *PlatformService) convertToPlatformModelSecrets(secretMap map[string]platform.Secret) []models.Secret {
secrets := []models.Secret{}
for _, v := range secretMap {
secrets = append(secrets, models.Secret{
return tool.MapToSlice(secretMap, func(key string, v platform.Secret) models.Secret {
return models.Secret{
Key: v.Key,
VaultId: v.Value,
VaultKey: v.VaultKey,
MaskValue: v.VaultMaskValue,
})
}

return secrets
}
})
}

func (s *PlatformService) convertToPlatformSecrets(ctx context.Context, secrets []models.Secret) (map[string]platform.Secret, error) {
Expand Down Expand Up @@ -158,24 +155,19 @@ func (s *PlatformService) convertToPlatformSecrets(ctx context.Context, secrets
}

func (s *PlatformService) convertToPlatformModelProperties(properties map[string]platform.Property) []models.Property {
wps := []models.Property{}
for _, v := range properties {
wps = append(wps, models.Property(v))
}

return wps
return tool.MapToSlice(properties, func(key string, env platform.Property) models.Property {
return models.Property(env)
})
}

func (s *PlatformService) convertToPlatformProperties(propertyList []models.Property) map[string]platform.Property {
properties := make(map[string]platform.Property)
for _, v := range propertyList {
properties[v.Key] = platform.Property{
Key: v.Key,
Value: v.Value,
}
}

return properties
return tool.SliceToMapWithTransform(propertyList, func(v models.Property) string { return v.Key },
func(v models.Property) platform.Property {
return platform.Property{
Key: v.Key,
Value: v.Value,
}
})
}

func (s *PlatformService) convertToPlatformViews(src []platform.Platform) []models.PlatformView {
Expand Down

0 comments on commit ead2078

Please sign in to comment.