mholt / json-to-go

Translates JSON into a Go type in your browser instantly (original)
https://mholt.github.io/json-to-go/
MIT License
4.48k stars 473 forks source link

non-anonymous struct regression #51

Closed suntong closed 5 years ago

suntong commented 5 years ago

The following json works for previous anonymous struct, but not the current non-anonymous structs. I just get undefined.

Would you double-check please? Thx!

{
  "id": "7a305b33-471c-42cf-9baf-f5220dc17180",
  "version": "1.1",
  "name": "Dummy",
  "url": "",
  "tests": [{
    "id": "3cd0b9fa-aba7-4f9a-b278-c5b43fd9b6be",
    "name": "SeleniumIDE",
    "commands": [{
      "id": "39b58390-385c-4a1e-8915-a993f08cc165",
      "comment": "",
      "command": "open",
      "target": "https://www.seleniumhq.org/",
      "targets": [],
      "value": ""
    }, {
      "id": "6458c851-6c8b-4b82-81d6-66eede61fa2f",
      "comment": "Goto \"Projects\"",
      "command": "echo",
      "target": "",
      "targets": [],
      "value": "Test Step 1"
    }, {
      "id": "bf6c20ea-1a4a-4235-969e-d2b04d193a5b",
      "comment": "",
      "command": "click",
      "target": "linkText=Projects",
      "targets": [
        ["linkText=Projects", "linkText"],
        ["css=a[title=\"Selenium Projects\"]", "css"],
        ["css=#menu_projects > a", "css:finder"],
        ["xpath=//a[contains(text(),'Projects')]", "xpath:link"],
        ["xpath=//li[@id='menu_projects']/a", "xpath:idRelative"],
        ["xpath=//a[contains(@href, '/projects/')]", "xpath:href"],
        ["xpath=//li[5]/a", "xpath:position"]
      ],
      "value": ""
    }, {
      "id": "b4e645f8-c16d-45a6-8116-c36b90ad3a68",
      "comment": "Goto \"Selenium IDE\"",
      "command": "echo",
      "target": "",
      "targets": [],
      "value": "Test Step 2"
    }, {
      "id": "ec99567d-e01b-46f8-b773-9a963ef9cc67",
      "comment": "Get into https://www.seleniumhq.org/selenium-ide/",
      "command": "click",
      "target": "linkText=Selenium IDE",
      "targets": [
        ["linkText=Selenium IDE", "linkText"],
        ["css=h3:nth-child(9) > a", "css:finder"],
        ["xpath=//a[contains(text(),'Selenium IDE')]", "xpath:link"],
        ["xpath=//div[@id='mainContent']/div/h3[3]/a", "xpath:idRelative"],
        ["xpath=//a[contains(@href, '/selenium-ide/')]", "xpath:href"],
        ["xpath=//h3[3]/a", "xpath:position"]
      ],
      "value": ""
    }, {
      "id": "d5886005-7d6d-4cbf-b400-1feaebb51546",
      "comment": "Goto \"Doc\"",
      "command": "echo",
      "target": "",
      "targets": [],
      "value": "Test Step 3"
    }, {
      "id": "18dc810f-2753-471c-b304-5ded76b99d60",
      "comment": "Get into https://www.seleniumhq.org/selenium-ide/docs/en/introduction/getting-started/",
      "command": "click",
      "target": "linkText=Docs",
      "targets": [
        ["linkText=Docs", "linkText"],
        ["css=li > a", "css"],
        ["css=li:nth-child(1) > a", "css:finder"],
        ["xpath=//a[contains(text(),'Docs')]", "xpath:link"],
        ["xpath=//a[contains(@href, '/selenium-ide/docs/en/introduction/getting-started')]", "xpath:href"],
        ["xpath=//li/a", "xpath:position"]
      ],
      "value": ""
    }]
  }],
  "suites": [{
    "id": "ef0df0fa-eb97-4d88-9e8c-b3aa0be8a687",
    "name": "Default Suite",
    "persistSession": false,
    "parallel": false,
    "timeout": 300,
    "tests": ["3cd0b9fa-aba7-4f9a-b278-c5b43fd9b6be"]
  }],
  "urls": [],
  "plugins": []
}

Originally posted by @suntong in https://github.com/mholt/json-to-go/issues/35#issuecomment-469879323

mike-hosseini commented 5 years ago

@suntong As mentioned in my comment https://github.com/mholt/json-to-go/issues/35#issuecomment-469889548, this is not a regression.

Also upon further investigation, I noticed that it works fine when I tested it using Node.js but not Google Chrome. This is the output I got:

type AutoGenerated struct {
    ID string `json:"id"`
    Version string `json:"version"`
    Name string `json:"name"`
    URL string `json:"url"`
    Tests [] `json:"tests"`
    Suites [] `json:"suites"`
    Urls []interface{} `json:"urls"`
    Plugins []interface{} `json:"plugins"`
}
type Commands struct {
    ID string `json:"id"`
    Comment string `json:"comment"`
    Command string `json:"command"`
    Target string `json:"target"`
    Targets []interface{} `json:"targets"`
    Value string `json:"value"`
}
type Tests struct {
    ID string `json:"id"`
    Name string `json:"name"`
    Commands [] `json:"commands"`
}
type Suites struct {
        ID string `json:"id"`
        Name string `json:"name"`
        PersistSession bool `json:"persistSession"`
        Parallel bool `json:"parallel"`
        Timeout int `json:"timeout"`
        Tests []string `json:"tests"`
}

So I think this issue can be closed. The solution I purpose is either to wait till it becomes available at https://mholt.github.io/json-to-go/ or clone the repository and run the code with Node.js.

mike-hosseini commented 5 years ago

Here is how to run it locally using Node.js:

  1. Install Node.js LTS from https://nodejs.org/en/
  2. Clone the json-to-go repository
  3. Create a app.js file in the same location as the json-to-go.js
  4. Create a new data.json file with your JSON data
  5. Paste the following in the app.js
    
    // app.js
    const jsonToGo = require('./json-to-go')

var goCode = new jsonToGo( require('fs').readFileSync("./data.json", "utf8") )

console.log(goCode.go)


6. Run `node app.js` from the terminal in the same folder
suntong commented 5 years ago

Sure. thanks for all the helps.

suntong commented 5 years ago

Are these output correct?

    Tests [] `json:"tests"`
    Suites [] `json:"suites"`

They are missing the variable names (or type definitions) in my view.

suntong commented 5 years ago

Confirmed, I got:

syntax error: unexpected literal `json:"tests"`, expecting type
syntax error: unexpected literal `json:"suites"`, expecting type
mike-hosseini commented 5 years ago

No, the output doesn't look correct. I will look into it.