package parse import ( "encoding/json" "errors" "fmt" "io" "go.mongodb.org/mongo-driver/v2/bson" ) type Op struct { UseTx bool `json:"tx"` Cmds []bson.D `json:"cmds"` } type JSONMigration struct { Up Op `json:"up"` Down Op `json:"down"` } func JSON(r io.Reader) (*JSONMigration, error) { bytes, err := io.ReadAll(r) if err != nil { return nil, err } var jf JSONMigration if err := json.Unmarshal(bytes, &jf); err != nil { return nil, err } if len(jf.Up.Cmds) == 0 { return nil, errors.New("missing 'up' commands") } if len(jf.Down.Cmds) == 0 { return nil, errors.New("missing 'down' commands") } if jf.Up.UseTx != jf.Down.UseTx { return nil, fmt.Errorf("up and down statements must have the same transaction mode") } return &jf, nil }