# ROM::Struct mapping question

**URL:** <https://discourse.rom-rb.org/t/rom-struct-mapping-question/633>\
**Category:** Issues\
**Created:** [January 2, 2024, 4:59pm UTC](https://discourse.rom-rb.org/t/rom-struct-mapping-question/633 "2024-01-02T16:59:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ck0jo](https://avatars.discourse-cdn.com/v4/letter/c/c67d28/32.png) [@ck0jo](https://discourse.rom-rb.org/u/ck0jo)\
**Post date:** [January 2, 2024, 4:59pm UTC](https://discourse.rom-rb.org/t/rom-struct-mapping-question/633/1 "2024-01-02T16:59:36Z")

</div>

Hello ROM Community,

I’m currently exploring ROM for database interactions and have encountered an issue with mapping database results to my custom entity, specifically regarding a custom enum type.

**Repository Setup:** I have a `Users::Repository` set up as follows:

```auto
# frozen_string_literal: true

require 'rom-repository'

module Users
  class Repository < ROM::Repository[:users]
    commands :create, update: :by_pk, delete: :by_pk

    struct_namespace Users::Entities
    auto_struct true
  end
end

```

I understood that `struct_namespace Users::Entities` would automatically map the database results to objects within the specified namespace.

**Entity Definition:** Here’s the definition of my `User` entity:

```auto
# frozen_string_literal: true

module Users
  module Entities
    class User < Users::Entity
      attribute :name, Types::String
      attribute :password, Types::String
      attribute :email, Types::String
      attribute :role, Types::Role
    end
  end
end

```

**Custom Type Definition:** The `Types::Role` is a custom enum type defined as:

```auto
module Types
    MEMBER = 'member'
    ADMIN = 'admin'

    Role = Types::String.default(MEMBER).enum(MEMBER => 0, ADMIN => 1).freeze
  end

```

**Issue:** When fetching a user from the repository with `user_repo.users.by_pk(6).one`, the `role` attribute doesn’t map to its string representation (‘member’ or ‘admin’) but remains as an integer:

```auto
#<Users::Entities::User name="Name" password="hashed_pwd" email="email@test.com" role=0 id=6>

```

However, when explicitly mapping to `Users::Entities::User` using `user_repo.users.by_pk(6).map_to(Users::Entities::User).one`, the mapping works correctly:

```auto
#<Users::Entities::User name="Name" password="hashed_pwd" email="email@test.com" role='member' id=6>

```

**Question:** Why doesn’t the automatic mapping via `struct_namespace` correctly apply my custom enum type? Is there a configuration step I’m missing, or is this an expected behavior requiring explicit mapping?

Any insights or suggestions on how to resolve this would be greatly appreciated.

Thank you!
