0

It's not the first time I'm trying to inherit from a class and convert an object to it, in this case extend functionalities of HTTP_CLIENT_RESPONSE with valid_http_response => response.status = 200, valid_http_json_body => validate json content, etc...

For that, I'd like to inherit from HTTP_CLIENT_RESPONSE and add features and create my custom DB_ENTITY_HTTP_CLIENT_RESPONSE from an HTTP_CLIENT_RESPONSE

test_case
    local
        l_http_client_r: HTTP_CLIENT_RESPONSE
        l_db_entity_http_client_r: DB_ENTITY_HTTP_CLIENT_RESPONSE
    do
        l_http_client_r := execute_get("someURL") -- returns an HTTP_CLIENT_RESPONSE
        l_db_entity_http_client_r := l_http_client_r
        assert("valid response", l_db_entity_http_client_r.valid_response)
    end

it seems I've difficulties having the internal properties set... what would be the best way to do this? I had the same case trying to create a WATT class inheriting from NATURAL_32 which is an expanded.

In my strategy, I tried to call in creator

  • a parent creation procedure
  • then call a deep_copy

Here is the rest of my class attempt:

class
    DB_ENTITY_HTTP_CLIENT_RESPONSE

inherit
    HTTP_CLIENT_RESPONSE

create
    make_from_http_client_response

convert
    make_from_http_client_response ({HTTP_CLIENT_RESPONSE})

feature -- Initialization

    make_from_http_client_response (a_client_response: HTTP_CLIENT_RESPONSE)
        do
            make (a_client_response.url)
            deep_copy (a_client_response)
        end

feature -- Status report

    valid_response: BOOLEAN
        do
            Result := status = 200
        end

The only way I found working for now is by setting all attributes to other which is the semantic of deep_copy normally...

make_from_http_client_response (a_client_response: HTTP_CLIENT_RESPONSE)
    do
        make (a_client_response.url)
        set_body (a_client_response.body)
        set_http_version (a_client_response.http_version)
        set_error_occurred (a_client_response.error_occurred)
        set_error_message (a_client_response.error_message)
        set_raw_header (a_client_response.raw_header)
        set_status_line (a_client_response.status_line)
        ... I surely forgot something...
    end
Pipo
  • 4,653
  • 38
  • 47

1 Answers1

1

There is no built-in feature to initialize an object of one type from the object of another type. The features copy and deep_copy expect objects of the same type. Therefore, setting attributes explicitly in the code is the way to go.

Another alternative is to employ client-supplier relationship instead of inheritance. Whether this is suitable, depends on the application.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35