Files
ResInsight/GrpcInterface/RiaGrpcServiceInterface.h
T
Kristian Bendiksen 71fa7d2c26 #12940 Add validation for Python GRPC field updates with atomic rollback
Implements comprehensive type and range validation to prevent invalid data
from being set via Python GRPC interface. Invalid values are rejected with
clear error messages. Uses two-phase commit pattern to ensure atomic updates
where either all fields update or none do.

Validation Features:
- Type validation: Ensures field value types match (parsing errors caught)
- Range validation: Checks values against min/max constraints defined with
  setRange(), setMinValue(), setMaxValue()
- Object validation: Validates cross-field constraints via validate() override
- Atomic updates: Two-phase commit ensures all-or-nothing semantics
- Rollback on error: Failed validation leaves ALL fields at previous values
- Clear error messages: Aggregates and returns all validation failures

Implementation:
- Use std::expected<void, QString> for type-safe error handling (C++23)
- assignFieldValue(): Validates after parsing, rolls back individual field on error
- copyPdmObjectFromRipsToCaf(): Two-phase commit pattern
  - Phase 1: Validate all fields and collect changes (defer UI notifications)
  - Phase 2: If any validation fails, rollback all fields; otherwise commit all
- UpdateExistingPdmObject(): Returns grpc::INVALID_ARGUMENT with validation details

Testing:
- Comprehensive Python tests demonstrating validation behavior
- Test atomic rollback: multiple fields rolled back when one fails
- No regression in existing functionality

Documentation:
- Added validation documentation to CLAUDE.md with examples
- Explains how to set field ranges and handle validation errors in Python

Built on validation infrastructure from commit f342ca77 (PdmFieldHandle and
PdmObjectHandle validation support).
2026-01-21 08:41:09 +01:00

78 lines
3.0 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2019- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
//////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <grpcpp/grpcpp.h>
#include <expected>
#include <vector>
class RiaGrpcCallbackInterface;
namespace caf
{
class PdmChildArrayFieldHandle;
class PdmChildFieldHandle;
class PdmFieldHandle;
class PdmObject;
class PdmObjectHandle;
class PdmScriptIOMessages;
} // namespace caf
namespace rips
{
class PdmObject;
}
class QString;
class QVariant;
//==================================================================================================
//
// gRPC-service interface which all gRPC-services has to implement
//
//==================================================================================================
class RiaGrpcServiceInterface
{
public:
virtual std::vector<RiaGrpcCallbackInterface*> createCallbacks() = 0;
virtual ~RiaGrpcServiceInterface() = default;
protected:
static void copyPdmObjectFromCafToRips( const caf::PdmObjectHandle* source, rips::PdmObject* destination );
static std::expected<void, QString> copyPdmObjectFromRipsToCaf( const rips::PdmObject* source,
caf::PdmObjectHandle* destination );
static caf::PdmObjectHandle*
emplaceChildField( caf::PdmObject* parent, const QString& fieldKeyword, const QString& keywordForClassToCreate );
static caf::PdmObjectHandle* emplaceChildField( caf::PdmChildFieldHandle* childField,
const QString& keywordForClassToCreate );
static caf::PdmObjectHandle* emplaceChildArrayField( caf::PdmChildArrayFieldHandle* childArrayField,
const QString& keywordForClassToCreate );
static std::expected<void, QString> assignFieldValue( const QString& stringValue,
caf::PdmFieldHandle* field,
QVariant* oldValue,
QVariant* newValue,
caf::PdmScriptIOMessages* messages );
};
#include "cafFactory.h"
using RiaGrpcServiceFactory = caf::Factory<RiaGrpcServiceInterface, size_t>;