Почему эти данные не могут быть правильно отправлены в мою объектную модель? - PullRequest
0 голосов
/ 03 октября 2018

У меня есть (не) рабочий пример: https://play.golang.org/p/qaYhKvJ65J3

Я не уверен, почему следующие данные:

alertData := `{
    "Id": 0,
    "Version": 0,
    "OrgId": 1,
    "DashboardId": 61,
    "PanelId": 84,
    "Name": "{qa-dev}{stats-pipeline} Topology Message Age (aggregator) alert",
    "Message": "",
    "Severity": "",
    "State": "",
    "Handler": 1,
    "Silenced": false,
    "ExecutionError": "",
    "Frequency": 10,
    "EvalData": null,
    "NewStateDate": "0001-01-01T00:00:00Z",
    "PrevStateDate": "0001-01-01T00:00:00Z",
    "StateChanges": 0,
    "Created": "0001-01-01T00:00:00Z",
    "Updated": "0001-01-01T00:00:00Z",
    "Settings": {
        "conditions": [
            {
                "evaluator": {
                    "params": [
                        10000
                    ],
                    "type": "gt"
                },
                "operator": {
                    "type": "and"
                },
                "query": {
                    "datasourceId": 2,
                    "model": {
                        "hide": true,
                        "refCount": 0,
                        "refId": "C",
                        "textEditor": false
                    },
                    "params": [
                        "C",
                        "5m",
                        "now"
                    ]
                },
                "reducer": {
                    "params": [],
                    "type": "avg"
                },
                "type": "query"
            }
        ],
        "executionErrorState": "keep_state",
        "frequency": "10s",
        "handler": 1,
        "name": "{qa-dev}{stats-pipeline} Topology Message Age (aggregator) alert",
        "noDataState": "keep_state",
        "notifications": []
    }
}`

Невозможно распаковать в следующий объектмодель:

type Condition struct {
    Evaluator struct {
        Params []int  `json:"params"`
        Type   string `json:"type"`
    } `json:"evaluator"`
    Operator struct {
        Type string `json:"type"`
    } `json:"operator"`
    Query struct {
        Params []string `json:"params"`
    } `json:"query"`
    Reducer struct {
        Params []interface{} `json:"params"`
        Type   string        `json:"type"`
    } `json:"reducer"`
    Type string `json:"type"`
}

Когда я делаю следующее:

condition := Condition{}
err := json.Unmarshal([]byte(alertData), &condition)

if err != nil {
    panic(err)
}

fmt.Printf("\n\n json object:::: %+v", condition)

Я просто получаю: json object:::: {Evaluator:{Params:[] Type:} Operator:{Type:} Query:{Params:[]} Reducer:{Params:[] Type:} Type:}

В идеале я мог бы разобрать это во что-токак type Conditions []struct{ } но я не уверен, что вы можете определить модели как списки?

Ответы [ 2 ]

0 голосов
/ 03 октября 2018

Maerics объяснение верно, вот альтернативный подход, который охватывает доступ к методам структуры, структура данных также полностью определена.Если вы новичок в Go, хорошо бы самостоятельно разобраться с созданием структур данных, но вот удобная утилита, помогающая создавать структуры из действительного JSON https://mholt.github.io/json-to-go/

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "time"
)

type Data struct {
    ID             int         `json:"Id"`
    Version        int         `json:"Version"`
    OrgID          int         `json:"OrgId"`
    DashboardID    int         `json:"DashboardId"`
    PanelID        int         `json:"PanelId"`
    Name           string      `json:"Name"`
    Message        string      `json:"Message"`
    Severity       string      `json:"Severity"`
    State          string      `json:"State"`
    Handler        int         `json:"Handler"`
    Silenced       bool        `json:"Silenced"`
    ExecutionError string      `json:"ExecutionError"`
    Frequency      int         `json:"Frequency"`
    EvalData       interface{} `json:"EvalData"`
    NewStateDate   time.Time   `json:"NewStateDate"`
    PrevStateDate  time.Time   `json:"PrevStateDate"`
    StateChanges   int         `json:"StateChanges"`
    Created        time.Time   `json:"Created"`
    Updated        time.Time   `json:"Updated"`
    Settings       struct {
        Conditions          []Condition   `json:"conditions"`
        ExecutionErrorState string        `json:"executionErrorState"`
        Frequency           string        `json:"frequency"`
        Handler             int           `json:"handler"`
        Name                string        `json:"name"`
        NoDataState         string        `json:"noDataState"`
        Notifications       []interface{} `json:"notifications"`
    } `json:"Settings"`
}

type Condition struct {
    Evaluator struct {
        Params []int  `json:"params"`
        Type   string `json:"type"`
    } `json:"evaluator"`
    Operator struct {
        Type string `json:"type"`
    } `json:"operator"`
    Query struct {
        DatasourceID int `json:"datasourceId"`
        Model        struct {
            Hide       bool   `json:"hide"`
            RefCount   int    `json:"refCount"`
            RefID      string `json:"refId"`
            TextEditor bool   `json:"textEditor"`
        } `json:"model"`
        Params []string `json:"params"`
    } `json:"query"`
    Reducer struct {
        Params []interface{} `json:"params"`
        Type   string        `json:"type"`
    } `json:"reducer"`
    Type string `json:"type"`
}

func (d Data) GetFirstCondition() (Condition, error) {
    if len(d.Settings.Conditions) > 0 {
        return d.Settings.Conditions[0], nil
    }
    return Condition{}, fmt.Errorf("no conditions found")
}

func (d Data) GetConditionByIndex(index uint) (Condition, error) {
    if len(d.Settings.Conditions) == 0 {
        return Condition{}, fmt.Errorf("no conditions found")
    }

    if int(index) > len(d.Settings.Conditions)-1 {
        return Condition{}, fmt.Errorf("index out of bounds")
    }

    return d.Settings.Conditions[index], nil
}

var alertData = `{
    "Id": 0,
    "Version": 0,
    "OrgId": 1,
    "DashboardId": 61,
    "PanelId": 84,
    "Name": "{qa-dev}{stats-pipeline} Topology Message Age (aggregator) alert",
    "Message": "",
    "Severity": "",
    "State": "",
    "Handler": 1,
    "Silenced": false,
    "ExecutionError": "",
    "Frequency": 10,
    "EvalData": null,
    "NewStateDate": "0001-01-01T00:00:00Z",
    "PrevStateDate": "0001-01-01T00:00:00Z",
    "StateChanges": 0,
    "Created": "0001-01-01T00:00:00Z",
    "Updated": "0001-01-01T00:00:00Z",
    "Settings": {
        "conditions": [
            {
                "evaluator": {
                    "params": [
                        10000
                    ],
                    "type": "gt"
                },
                "operator": {
                    "type": "and"
                },
                "query": {
                    "datasourceId": 2,
                    "model": {
                        "hide": true,
                        "refCount": 0,
                        "refId": "C",
                        "textEditor": false
                    },
                    "params": [
                        "C",
                        "5m",
                        "now"
                    ]
                },
                "reducer": {
                    "params": [],
                    "type": "avg"
                },
                "type": "query"
            }
        ],
        "executionErrorState": "keep_state",
        "frequency": "10s",
        "handler": 1,
        "name": "{qa-dev}{stats-pipeline} Topology Message Age (aggregator) alert",
        "noDataState": "keep_state",
        "notifications": []
    }
}`

func main() {
    var res Data
    err := json.Unmarshal([]byte(alertData), &res)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(res.GetFirstCondition())
    fmt.Println(res.GetConditionByIndex(0))
    // should fail :-)
    fmt.Println(res.GetConditionByIndex(1))

}
0 голосов
/ 03 октября 2018

Похоже, вы пытаетесь получить доступ к свойству «условиям», вложенному в корневое свойство «Настройки».Таким образом, вам нужно определить этот тип корневого уровня и достаточное количество полей, чтобы сообщить демонологу, как найти целевое свойство.Таким образом, вам просто нужно создать новый тип «AlertData» с необходимыми полями «Настройки / условия».

Например ( Go Playground ):

type AlertData struct {
  Settings struct {
    Conditions []Condition `json:"conditions"`
  }
}

func main() {
  alert := AlertData{}
  err := json.Unmarshal([]byte(alertData), &alert)

  if err != nil {
    panic(err)
  }

  fmt.Printf("OK: conditions=%#v\n", alert.Settings.Conditions)
  // OK: conditions=[]main.Condition{main.Condition{Evaluator:struct { Params []int "json:\"params\""; Type string "json:\"type\"" }{Params:[]int{10000}, Type:"gt"}, Operator:struct { Type string "json:\"type\"" }{Type:"and"}, Query:struct { Params []string "json:\"params\"" }{Params:[]string{"C", "5m", "now"}}, Reducer:struct { Params []interface {} "json:\"params\""; Type string "json:\"type\"" }{Params:[]interface {}{}, Type:"avg"}, Type:"query"}}
}

Обратите внимание, что в распечатанном листинге содержится так много информации о типах, потому что тип "Condition" использует анонимные структуры в качестве типов полей.Если бы вы извлекли их в именованные структуры, вам будет легче работать с данными, например:

type Condition struct {
  Evaluator Evaluator `json:"evaluator"`
  Operator  Operator  `json:"operator"`
  // ...
}

type Evaluator struct {
  Params []int  `json:"params"`
  Type   string `json:"type"`
}

type Operator struct {
  Type string `json:"type"`
}

//...
// OK: conditions=[]main.Condition{
//   main.Condition{
//     Evaluator:main.Evaluator{Params:[]int{10000}, Type:"gt"},
//     Operator:main.Operator{Type:"and"},
//     Query:main.Query{Params:[]string{"C", "5m", "now"}},
//     Reducer:main.Reducer{Params:[]interface {}{}, Type:"avg"},
//     Type:"query",
//   },
// }

Go Go Playground пример здесь ...

...