function list = getParamsFromFile(this, file)
% A convenience func to get the list of params from file

%   Copyright 2013-2021 The MathWorks, Inc.

    list = struct;
    try
        [status, params, errparams, err] = this.loadParamsFile(file);
    catch me
        rethrow(me);
    end

    if status
        if ~isempty(params)
            paramnames = fieldnames(params);
            for i = 1:length(paramnames)
                if slavteng('feature','BusParameterTuning') && isstruct(params.(paramnames{i}))
                    list.(paramnames{i}) = createConstraintsList(params.(paramnames{i}), paramnames{i});
                else
                    list.(paramnames{i}) = Sldv.ParameterTuning.Param(paramnames{i},...
                                                                      'Constraint', params.(paramnames{i}));
                end
            end
        end
    else
        if ~isempty(errparams)
            flds = fieldnames(errparams);
            pnames = strjoin(flds, ',');
            error('Sldv:Parameters:SyntaxError', message('Sldv:Parameters:SyntaxError', pnames).getString);

        end
    end
end

function spec = createConstraintsList(paramSpec, paramName)
% Creates individual entries for all the leaves of each and every
% parameter
    if isstruct(paramSpec)
        leavesOfParam = fieldnames(paramSpec);
        for idx = 1:length(leavesOfParam)
            currentFieldName = [paramName '.' leavesOfParam{idx}];
            spec.(leavesOfParam{idx}) = createConstraintsList(paramSpec.(leavesOfParam{idx}), currentFieldName);
        end
    else
        spec =  Sldv.ParameterTuning.Param(paramName, 'Constraint', paramSpec);
    end
end
