% MJSSchemaHelper - Static helper class for common operations required for
% constructing and parsing method inputs and output built from client
% schemas

% Copyright 2020 The MathWorks, Inc.

classdef (Sealed, Hidden) MJSSchemaHelper
    
    properties (Constant)
        % TypeUrlField - Expected name of field describing schema name
        TypeUrlField = 'type_url';
        % SchemaPackageName - Name of schema package prefixing type name
        SchemaPackageName = 'parallel.mjsmessages';
    end
    
    methods (Static)
        function fullName = getFullName(name)
            % Return full schema name from short name
            fullName = [parallel.internal.cluster.MJSSchemaHelper.SchemaPackageName '.' name];
        end
        
        function validateFieldValue(structToCheck, fieldName, fieldValue)
            % Validate struct field name exists and has expected value
            assert(isfield(structToCheck, fieldName), ['Expected field not found: ' fieldName]);
            assert(isequal(structToCheck.(fieldName), fieldValue), ['Unexpected field value: ' fieldName]);
        end

        function validateMessageName(structToCheck, expectedName)
            % Validate struct has schema type name matching given short
            % name
            import parallel.internal.cluster.MJSSchemaHelper;
            
            MJSSchemaHelper.validateFieldValue(structToCheck, ...
                MJSSchemaHelper.TypeUrlField, MJSSchemaHelper.getFullName(expectedName));
        end
        
        function validateFullMessageName(structToCheck, expectedFullName)
            % Validate struct has schema type name matching given full
            % name
            import parallel.internal.cluster.MJSSchemaHelper;
            
            MJSSchemaHelper.validateFieldValue(structToCheck, ...
                MJSSchemaHelper.TypeUrlField, expectedFullName);
        end
    end
end

