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

fix: Bug with misplaced Go time imports #2531

Closed rekram1-node closed 3 months ago

rekram1-node commented 4 months ago

fixes: #2530

My previous fix forced generated Go code to add time imports for []time.Time types, however, the change was missing an additional update included in this fix which will ensure that the import takes place at the top of the file (otherwise it would encounter a syntax error)

Changes:

Reproduction to fix bug:

Schema:

{
    "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 "time"

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)
}

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

Notice: time import is where it should be, fixing #2530

rekram1-node commented 3 months ago

@dvdsgl Sorry to bug you just wanted to see if we could merge this change soon, this adds an additional fix that my previous pull request was missing

dvdsgl commented 3 months ago

Thank you @rekram1-node !