Friday, May 17, 2024
HomeGolangHow you can change and delete values in a struct - Getting...

How you can change and delete values in a struct – Getting Assist


I would like to alter the title of the keys in a struct by means of go.

I’ve this operate

func (p *DbConfig) myfunction(prefix string, namespace string, dbName string, userSecret string) []corev1.EnvVar {
	   prefix = Underscore(prefix)
	   namespace = Underscore(namespace)
        

        envs := filter(p.myfunction(prefix), func(merchandise corev1.EnvVar) bool {
		if merchandise.Identify == "USER" {
			return false
		}
		if merchandise.Identify == "PASSWORD" {
			return false
		}
		return true
	 })


	return append(envs, []corev1.EnvVar{
		{
			Identify:  prefix + "DB_NAME",
			Worth: namespace + dbName,
		},
		{
			Identify: prefix + "DB_USER",
			ValueFrom: &corev1.EnvVarSource{
				SecretKeyRef: &corev1.SecretKeySelector{
					LocalObjectReference: corev1.LocalObjectReference{
						Identify: userSecret,
					},
					Key: "POSTGRES_USER",
				},
			},
		},
		{
			Identify: prefix + "DB_PASSWORD",
			ValueFrom: &corev1.EnvVarSource{
				SecretKeyRef: &corev1.SecretKeySelector{
					LocalObjectReference: corev1.LocalObjectReference{
						Identify: userSecret,
					},
					Key: "POSTGRES_PASSWORD",
				},
			},
		},
	}...)
}

The place EnVar struct appears like

kind EnvVar struct {
    Identify string `json:"title" protobuf:"bytes,1,decide,title=title"`

	Worth string `json:"worth,omitempty" protobuf:"bytes,2,decide,title=worth"`

	ValueFrom *EnvVarSource `json:"valueFrom,omitempty" protobuf:"bytes,3,decide,title=valueFrom"`
}

kind EnvVarSource struct {


	FieldRef *ObjectFieldSelector `json:"fieldRef,omitempty" protobuf:"bytes,1,decide,title=fieldRef"`

	ResourceFieldRef *ResourceFieldSelector `json:"resourceFieldRef,omitempty" protobuf:"bytes,2,decide,title=resourceFieldRef"`

	ConfigMapKeyRef *ConfigMapKeySelector `json:"configMapKeyRef,omitempty" protobuf:"bytes,3,decide,title=configMapKeyRef"`

	SecretKeyRef *SecretKeySelector `json:"secretKeyRef,omitempty" protobuf:"bytes,4,decide,title=secretKeyRef"`
}

and right here I would like to alter the values containing “CONTAINER” with the right env vars used within the container.

func containerEnvs(hub *immv1alpha1.ImmHub, occasion *immv1alpha1.Container) []corev1.EnvVar {
	postgresService := hub.Spec.DbConfig.myfunction("CONTAINER_", hub.Namespace, occasion.Spec.dbName, occasion.Spec.userSecret)
    
    //Right here I need to modify the keys of the struct that I would like in order that appropriate atmosphere variables are handed

	postgresService[0].Identify = "DATABASE_NAME"
	postgresService[1].Identify = "DATABASE_USER"

	service := []corev1.EnvVar{
		{
			Identify:  "DATABASE_NAME",
			Worth: occasion.Spec.dbName,
		}
	}
	return append(service, append(postgresService)...)

}

The output of the postgresService variable appears like

[{CONTAINER_DB_NAME testdb nil} {CONTAINER_DB_USER  &EnvVarSource{FieldRef:nil,ResourceFieldRef:nil,ConfigMapKeyRef:nil,SecretKeyRef:&SecretKeySelector{LocalObjectReference:LocalObjectReference{Name:secret,},Key:POSTGRES_USER,Optional:nil,},}} {CONTAINER_DB_PASSWORD  &EnvVarSource{FieldRef:nil,ResourceFieldRef:nil,ConfigMapKeyRef:nil,SecretKeyRef:&SecretKeySelector{LocalObjectReference:LocalObjectReference{Name:secret,},Key:POSTGRES_PASSWORD,Optional:nil,},}}]

I’ve two issues right here:

1- Is that this one of the best ways to alter the default key names in my case?

2- If the variable postgresService incorporates extra values however I simply want the DATABASE_NAME and DATABASE_USER how can I delete the remainder of the values from postgresService?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments