Skip to content

initial commit for supporting custom string and intarray#1

Open
hazyd wants to merge 6 commits intomainfrom
su
Open

initial commit for supporting custom string and intarray#1
hazyd wants to merge 6 commits intomainfrom
su

Conversation

@hazyd
Copy link
Owner

@hazyd hazyd commented Sep 3, 2022

No description provided.

@staticmethod
def ddl() -> str:
def ddl(len: int = 0) -> str:
return 'ARRAY<STRING(MAX)>'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should respect string length.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@staticmethod
def ddl() -> str:
def ddl(len: int = 0) -> str:
return 'BYTES(MAX)'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should respect string length

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

for field_type in field.ALL_TYPES:
if self.spanner_type == field_type.ddl():
len = _get_str_len(self.spanner_type)
if self.spanner_type == field_type.ddl(len):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, leave field_type.ddl() to always return string with (MAX). And then you just replace (len) with (MAX) in the spanner_type and compare that. You can add a property supports_length which returns bool when this substitution is needed, to avoid replacing MAX in all possible types.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

I didn't add property. Instead, I added a check in the init function. If the type is not length_support, the length is always 0 and we skip the string replacement.

if self._nullable:
return self._type.ddl()
return '{field_type} NOT NULL'.format(field_type=self._type.ddl())
return self._type.ddl(self._length)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a bit cleaner would be to let _type.ddl() to return MAX and then if the length is defined, do str replace for (MAX) to (len). This way you don't have to pass the length to static ddl method in all classes.

You can also add a property called supports_length to the field type, and check it - if its false and len is non zero - throw an exception.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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.

2 participants