Skip to content

Conversation

@BaptisteGi
Copy link
Contributor

  • I created a class for each possible kind
  • Then had to append "Attribute" to the name to avoid collisions
  • So it generates protocols with exact same type as user would expect it to be from the schema
  • Then it's mapped to something else (e.g. str) behind the scene

Example generated:

class ExampleFullAttributesNode(CoreNode):
    id_required: IDAttribute
    id_optional: IDAttributeOptional
    text_required: TextAttribute
    text_optional: TextAttributeOptional
    textarea_required: TextAreaAttribute
    textarea_optional: TextAreaAttributeOptional
    datetime_required: DateTimeAttribute
    datetime_optional: DateTimeAttributeOptional
    email_required: EmailAttribute
    email_optional: EmailAttributeOptional
    password_required: PasswordAttribute
    password_optional: PasswordAttributeOptional
    hashed_password_required: HashedPasswordAttribute
    hashed_password_optional: HashedPasswordAttributeOptional
    url_required: URLAttribute
    url_optional: URLAttributeOptional
    file_required: FileAttribute
    file_optional: FileAttributeOptional
    mac_address_required: MacAddressAttribute
    mac_address_optional: MacAddressAttributeOptional
    color_required: ColorAttribute
    color_optional: ColorAttributeOptional
    number_required: NumberAttribute
    number_optional: NumberAttributeOptional
    bandwidth_required: BandwidthAttribute
    bandwidth_optional: BandwidthAttributeOptional
    ip_host_required: IPHostAttribute
    ip_host_optional: IPHostAttributeOptional
    ip_network_required: IPNetworkAttribute
    ip_network_optional: IPNetworkAttributeOptional
    boolean_required: BooleanAttribute
    boolean_optional: BooleanAttributeOptional
    checkbox_required: CheckboxAttribute
    checkbox_optional: CheckboxAttributeOptional
    list_required: ListAttribute
    list_optional: ListAttributeOptional
    json_required: JSONAttribute
    json_optional: JSONAttributeOptional
    any_required: AnyAttribute
    any_optional: AnyAttributeOptional
    dropdown_required: DropdownAttribute
    dropdown_optional: DropdownAttributeOptional
    profiles: RelationshipManager
    member_of_groups: RelationshipManager
    subscriber_of_groups: RelationshipManager

@BaptisteGi BaptisteGi changed the base branch from develop to stable October 17, 2024 15:15
@BaptisteGi BaptisteGi self-assigned this Oct 17, 2024
@BaptisteGi
Copy link
Contributor Author

Should I drop all the classes I've created to only use the "primitive" ones and map everything to one of those:

class String(Attribute):
    value: str


class StringOptional(Attribute):
    value: Optional[str]


class Integer(Attribute):
    value: int


class IntegerOptional(Attribute):
    value: Optional[int]


class Boolean(Attribute):
    value: bool


class BooleanOptional(Attribute):
    value: Optional[bool]


class DateTime(Attribute):
    value: str


class DateTimeOptional(Attribute):
    value: Optional[str]


class Enum(Attribute):
    value: str


class EnumOptional(Attribute):
    value: Optional[str]


class URL(Attribute):
    value: str


class URLOptional(Attribute):
    value: Optional[str]


class Dropdown(Attribute):
    value: str


class DropdownOptional(Attribute):
    value: Optional[str]


class IPNetwork(Attribute):
    value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network]


class IPNetworkOptional(Attribute):
    value: Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]


class IPHost(Attribute):
    value: Union[ipaddress.IPv4Address, ipaddress.IPv6Address]


class IPHostOptional(Attribute):
    value: Optional[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]


class HashedPassword(Attribute):
    value: str


class HashedPasswordOptional(Attribute):
    value: Any


class JSONAttribute(Attribute):
    value: Any


class JSONAttributeOptional(Attribute):
    value: Optional[Any]


class ListAttribute(Attribute):
    value: list[Any]


class ListAttributeOptional(Attribute):
    value: Optional[list[Any]]

@BaptisteGi BaptisteGi marked this pull request as ready for review October 17, 2024 16:37
@dgarros
Copy link
Contributor

dgarros commented Oct 17, 2024

I'm not sure if there is a strong case to keep the existing one over yours ... either should be fine
In some way your approach is probably cleaner as it doesn't require us to maintain a mapping
but either way we should clean up the other type to avoid having redundant attribute classes

@BaptisteGi
Copy link
Contributor Author

I'm not sure if there is a strong case to keep the existing one over yours ... either should be fine In some way your approach is probably cleaner as it doesn't require us to maintain a mapping but either way we should clean up the other type to avoid having redundant attribute classes

Ok, I tried to consolidate to the new class I've created but it roots to invoke backend.generate in the main infrahub project, not quite sure of the impact of changing things there ...

So for the time being I mapped everything to the "primitive" types and I'll create a task to cleanup that later.

Here is the final result:

class ExampleFullAttributesNode(CoreNode):
    mac_address_optional: MacAddressOptional
    textarea_required: String
    datetime_optional: DateTimeOptional
    any_required: AnyAttribute
    dropdown_optional: DropdownOptional
    json_optional: JSONAttributeOptional
    color_required: String
    ip_network_required: IPNetwork
    text_optional: StringOptional
    file_required: String
    password_required: String
    hashed_password_required: HashedPassword
    number_optional: IntegerOptional
    dropdown_required: Dropdown
    hashed_password_optional: HashedPasswordOptional
    url_required: URL
    id_required: String
    boolean_optional: BooleanOptional
    boolean_required: Boolean
    password_optional: StringOptional
    email_optional: StringOptional
    textarea_optional: StringOptional
    color_optional: StringOptional
    checkbox_required: Boolean
    bandwidth_optional: IntegerOptional
    ip_host_required: IPHost
    bandwidth_required: Integer
    any_optional: AnyAttributeOptional
    number_required: Integer
    checkbox_optional: BooleanOptional
    id_optional: StringOptional
    ip_network_optional: IPNetworkOptional
    list_optional: ListAttributeOptional
    file_optional: StringOptional
    list_required: ListAttribute
    json_required: JSONAttribute
    datetime_required: DateTime
    email_required: String
    mac_address_required: MacAddress
    text_required: String
    url_optional: URLOptional
    ip_host_optional: IPHostOptional
    subscriber_of_groups: RelationshipManager
    profiles: RelationshipManager
    member_of_groups: RelationshipManager

@dgarros
Copy link
Contributor

dgarros commented Oct 18, 2024

Side note, would be good to have a unit tests to validate that ATTRIBUTE_KIND_MAP includes all the supported kinds from the schema
not a blocker for this PR but would be good to create a task to track that

@BaptisteGi BaptisteGi merged commit cc169f3 into stable Oct 21, 2024
10 checks passed
@BaptisteGi BaptisteGi deleted the bgi-protocols-supports-all-attributes-kind-57 branch October 21, 2024 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants