VSChina / magic-modules

Magic Modules: Automagically generate Google Cloud Platform support for OSS
Apache License 2.0
1 stars 4 forks source link

[Feature Request] Error handling for expand and flatten functions #36

Open JunyiYi opened 5 years ago

JunyiYi commented 5 years ago

Today MM will not check errors in expand or flatten, which is not always correct. For example, to expand/flatten a date.Time type:

func expandStringToDate(input interface{}) (*date.Time, error) {
    v := input.(string)

    dateTime, err := date.ParseTime(time.RFC3339, v)
    if err != nil {
        return nil, err
    }

    result := date.Time{
        Time: dateTime,
    }
    return &result, nil
}

Actual Result

When we call it in the function, MM will generate something like

parameters = xxx.yyy {
    MyDate: expandStringToDate(dt)
};

Expected Result

But ideally, it should generate:

parameters = xxx.yyy {};

if date, err := expandStringToDate(dt); err != nil {
    return err
} else {
    parameters.MyDate = date;
}

Notice that we should propagate all errors up until the top level instead of handling it silently in sub-functions.

Proposed Solution

We need to introduce some special flags to indicate whether an expand/flatten function returns error or not, and handle that in the caller.