Skip to content

Instantly share code, notes, and snippets.

@ardentperf
Created February 8, 2025 07:53
Show Gist options
  • Select an option

  • Save ardentperf/a32a5a68a805bb1dc6c6523cbed51899 to your computer and use it in GitHub Desktop.

Select an option

Save ardentperf/a32a5a68a805bb1dc6c6523cbed51899 to your computer and use it in GitHub Desktop.
 cat jtest.go
package main
import (
"encoding/json"
"fmt"
cnpg "github.com/cloudnative-pg/cloudnative-pg/api/v1" // Import the CNPG API types
)
// Sample map[string]interface{} representing a CNPG ClusterSpec
var data = map[string]interface{}{
"instances": 3,
"imageName": "ghcr.io/cloudnative-pg/postgresql:15",
"bootstrap": map[string]interface{}{
"initdb": map[string]interface{}{
"database": "mydb",
"owner": "myuser",
"secret": map[string]interface{}{
"name": "my-cluster-secret",
},
},
},
"storage": map[string]interface{}{
"size": "10Gi",
},
}
func main() {
// Convert map[string]interface{} to JSON
jsonData, err := json.Marshal(data)
if err != nil {
fmt.Println("Error marshaling to JSON:", err)
return
}
// Convert JSON to ClusterSpec struct
var clusterSpec cnpg.ClusterSpec
err = json.Unmarshal(jsonData, &clusterSpec)
if err != nil {
fmt.Println("Error unmarshaling JSON to ClusterSpec:", err)
return
}
// Successfully converted
fmt.Printf("Converted ClusterSpec: %+v\n", clusterSpec)
}
 go build
 ./jtest
Converted ClusterSpec: {Description: InheritedMetadata:<nil> ImageName:ghcr.io/cloudnative-pg/postgresql:15 ImageCatalogRef:<nil> ImagePullPolicy: SchedulerName: PostgresUID:0 PostgresGID:0 Instances:3 MinSyncReplicas:0 MaxSyncReplicas:0 PostgresConfiguration:{Parameters:map[] Synchronous:<nil> PgHBA:[] PgIdent:[] SyncReplicaElectionConstraint:{NodeLabelsAntiAffinity:[] Enabled:false} AdditionalLibraries:[] LDAP:<nil> PgCtlTimeoutForPromotion:0 EnableAlterSystem:false} ReplicationSlots:<nil> Bootstrap:0xc0003fc390 ReplicaCluster:<nil> SuperuserSecret:<nil> EnableSuperuserAccess:<nil> Certificates:<nil> ImagePullSecrets:[] StorageConfiguration:{StorageClass:<nil> Size:10Gi ResizeInUseVolumes:<nil> PersistentVolumeClaimTemplate:nil} ServiceAccountTemplate:<nil> WalStorage:<nil> EphemeralVolumeSource:nil MaxStartDelay:0 MaxStopDelay:0 SmartShutdownTimeout:<nil> MaxSwitchoverDelay:0 FailoverDelay:0 LivenessProbeTimeout:<nil> Affinity:{EnablePodAntiAffinity:<nil> TopologyKey: NodeSelector:map[] NodeAffinity:nil Tolerations:[] PodAntiAffinityType: AdditionalPodAntiAffinity:nil AdditionalPodAffinity:nil} TopologySpreadConstraints:[] Resources:{Limits:map[] Requests:map[] Claims:[]} EphemeralVolumesSizeLimit:<nil> PriorityClassName: PrimaryUpdateStrategy: PrimaryUpdateMethod: Backup:<nil> NodeMaintenanceWindow:<nil> Monitoring:<nil> ExternalClusters:[] LogLevel: ProjectedVolumeTemplate:nil Env:[] EnvFrom:[] Managed:<nil> SeccompProfile:nil Tablespaces:[] EnablePDB:<nil> Plugins:[] Probes:<nil>}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment