glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
11.77k stars 1.04k forks source link

Go time import is misplaced in some circumstances. #2530

Closed rekram1-node closed 3 months ago

rekram1-node commented 4 months ago

My fix for #2528 fixed the bug where Go code was missing the time import. However, it seems a new bug was introduced where in some cases the time import is not appearing at the top of the generated file.

Reproduction steps:

{
    "type": "object",
    "properties": {
        "timeRange": {
            "type": "array",
            "items": {
                "type": "string",
                "format": "date-time"
            }
        }
    }
}

Generated Code:

// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse and unparse this JSON data, add this code to your project and do:
//
//    models, err := UnmarshalModels(bytes)
//    bytes, err = models.Marshal()

package generated

import "encoding/json"

func UnmarshalModels(data []byte) (Models, error) {
    var r Models
    err := json.Unmarshal(data, &r)
    return r, err
}

func (r *Models) Marshal() ([]byte, error) {
    return json.Marshal(r)
}

import "time"

type Models struct {
    TimeRange []time.Time `json:"timeRange,omitempty"`
}

Note the time import is not at the top of the file like the other import. This is a bug that causes the go code to not compile (syntax error).

Looking into a fix for this hopefully will have a Pull Request ready shortly.

rekram1-node commented 4 months ago

Found the issue, basically just a 1 liner fix that I missed the first time around.