Go JSON parsing error

Moshe Shmukler :

I am writing my first program in Golang, or rather modifying a template, if you will. Want to parse values from a yaml file.

apiVersion: "backend.example.com/v1alpha1"
kind: "Example"
metadata:
  name: "solar-demo"
spec:
  size: 2
  group: backend.example.com
  names:
    kind: Example
    listKind: ExampleList
    plural: solar-demos
    singular: solar-demo
  scope: Namespaced
  version: v1alpha1
  pods:
    - name: api
      image: "shmukler/docker_solar-api"
      port: 3000
      command: '{"npm", "run", "start-api"}'
    - name: service
      image: "shmukler/docker_solar-svc"
      port: 3001
      command: '{"npm", "run", "start-solar-svc"}'

To parse-out variables, I do:

type Example struct {
    metav1.TypeMeta   `json:",inline"`
    metav1.ObjectMeta `json:"metadata"`
    Spec              ExampleSpec   `json:"spec"`
    Status            ExampleStatus `json:"status,omitempty"`
}
type ExampleSpec struct {
    Size int32 `json:"size"`
    //  Pods []ExamplePod `json:"pods"`
}
type ExampleStatus struct {
    Nodes []string `json:"nodes"`
}
type ExamplePod struct {
    Name    []string `json:"name"`
    Image   []string `json:"image"`
    Port    int32    `json:"port"`
    Command []string `json:"command"`
}

If I comment out the Pods []ExamplePod line, the code works albeit without parsing the Pods array. If I leave it in, there is an error, as soon as I pass the yaml document - Observed a panic: &errors.errorString{s:"failed to decode json data with gvk...

I want to stick pods into an array of structures of the type ExamplePod.

Today is my first day working with Go language. Sorry for a stupid question. My yaml, the relevant fragment in JSON representation.

spec":{  
   "group":"backend.example.com",
   "names":{  
      "kind”:”Example”,
      "listKind”:”ExampleList",
      "plural":"solar-demos",
      "singular":"solar-demo"
   },
   "pods":[  
      {  
         "command":"{\"npm\", \"run\", \"start-api\"}",
         "image":"shmukler/docker_solar-api",
         "name":"api",
         "port":3000
      },
      {  
         "command":"{\"npm\", \"run\", \"start-solar-svc\"}",
         "image":"shmukler/docker_solar-svc",
         "name":"service",
         "port":3001
      }
   ],
   "scope":"Namespaced",
   "size":2,
   "version":"v1alpha1"
}

I am able to get example.Spec.Size just fine. What I am missing is the pods array.

Moshe Shmukler :

Using the suggestions from both offered answers changed the YAML to:

containers:
- name: api
  image: "shmukler/docker_solar-api"
  port: 3000
  command: ["npm", "run", "start-api"]
- name: service
  image: "shmukler/docker_solar-svc"
  port: 3001
  command: ["npm", "run", "start-solar-svc"]

Then, updated definitions to:

type Spec struct {
    Size       int32               `json:"size"`
    Containers []Container `json:"containers"`
}
type Status struct {
    Nodes []string `json:"nodes"`
}
type Container struct {
    Name    string   `json:"name"`
    Image   string   `json:"image"`
    Port    int32    `json:"port"`
    Command []string `json:"command"`
}

That finally worked fine.

Thank you for the answers, helped me to understand how Golang type definitions work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related