mirror of
https://github.com/OMGeeky/google-apis-rs.git
synced 2026-01-25 04:35:18 +01:00
fix some enums getting wrong names
(some were way too long)
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
from typing import Any
|
||||
from .rust_type import RustType
|
||||
from .types import Base
|
||||
from .util import Context, UNUSED_TYPE_MARKER, schema_markers, canonical_type_name, remove_invalid_chars_in_ident, \
|
||||
singular
|
||||
singular, activity_split
|
||||
|
||||
|
||||
def is_property_enum(s: dict) -> bool:
|
||||
@@ -14,7 +15,7 @@ def is_property_enum(s: dict) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def get_enum_type(schema_name: str, property_name: str, t, s) -> RustType:
|
||||
def get_enum_type(schema_name: str, property_name: str) -> RustType:
|
||||
if schema_name is None:
|
||||
# print('schema_name was None:', property_name, t)
|
||||
schema_name = ""
|
||||
@@ -29,18 +30,13 @@ def get_enum_type(schema_name: str, property_name: str, t, s) -> RustType:
|
||||
return Base(name)
|
||||
|
||||
|
||||
def _add_enum_value(k, pk, pv, enums: dict):
|
||||
if is_property_enum(pv):
|
||||
enum_type = get_enum_type(k, pk, None, None)
|
||||
# print('enum type:', enum_type)
|
||||
enums[enum_type] = (k, pk, enum_type, pv)
|
||||
return enums
|
||||
def get_enum_if_is_enum(k, property_name: str, property_value: dict) -> RustType | None:
|
||||
if property_value is None:
|
||||
return None
|
||||
if is_property_enum(property_value):
|
||||
return get_enum_type(k, property_name)
|
||||
|
||||
inner_pv = _get_inner_enum(pv)
|
||||
if inner_pv:
|
||||
return _add_enum_value(k, pk, inner_pv, enums)
|
||||
|
||||
return enums
|
||||
return get_enum_if_is_enum(k, property_name, _get_inner_enum(property_value))
|
||||
|
||||
|
||||
def _get_inner_enum(pv: dict):
|
||||
@@ -54,52 +50,44 @@ def _get_inner_enum(pv: dict):
|
||||
return None
|
||||
|
||||
|
||||
def _parse_method_id(method_id: str, c: Context) -> str:
|
||||
parts = method_id.split('.')
|
||||
if len(parts) != 3:
|
||||
return method_id
|
||||
context = c.rtc_map.get(parts[1])
|
||||
|
||||
if context is None or parts[0] != context:
|
||||
print('context was not equal to first part: ', context, parts[0], method_id)
|
||||
return method_id
|
||||
def find_enums_in_context(c: Context) -> list[tuple[str, Any, RustType, Any]]:
|
||||
enums: dict[tuple, tuple[str, Any, RustType, Any]] = {}
|
||||
for name, s in c.schemas.items():
|
||||
if UNUSED_TYPE_MARKER in schema_markers(s, c, transitive=True):
|
||||
continue
|
||||
|
||||
methods = c.rta_map.get(parts[1])
|
||||
if methods is None or parts[2] not in methods:
|
||||
print('third part was not in methods', methods, parts[2], method_id)
|
||||
properties = s.get('properties')
|
||||
if not properties:
|
||||
continue
|
||||
|
||||
return parts[1]
|
||||
try:
|
||||
p = properties.to_dict()
|
||||
vals = p.values()
|
||||
keys = p.keys()
|
||||
|
||||
for pv, pk in zip(vals, keys):
|
||||
enum = get_enum_if_is_enum(name, pk, pv)
|
||||
if enum:
|
||||
enums[(name, pk)] = (name, pk, enum, pv)
|
||||
except TypeError as e:
|
||||
print('exception in find_enums_in_context:', e)
|
||||
print('name:', name)
|
||||
print('s:', s)
|
||||
print('props:', properties)
|
||||
print('props type:', type(properties))
|
||||
raise e
|
||||
|
||||
def find_enums_in_context(c: Context) -> list:
|
||||
enums = {}
|
||||
for k, s in c.schemas.items():
|
||||
if UNUSED_TYPE_MARKER not in schema_markers(s, c, transitive=True):
|
||||
properties = s.get('properties')
|
||||
# printed_name = False
|
||||
if properties:
|
||||
try:
|
||||
p = properties.to_dict()
|
||||
vals = p.values()
|
||||
keys = p.keys()
|
||||
|
||||
for pv, pk in zip(vals, keys):
|
||||
enums = _add_enum_value(k, pk, pv, enums)
|
||||
except TypeError as e:
|
||||
print('exception in find_enums_in_context:', e)
|
||||
print('k:', k)
|
||||
print('s:', s)
|
||||
print('props:', properties)
|
||||
print('props type:', type(properties))
|
||||
# print('c:', c)
|
||||
raise e
|
||||
|
||||
for k, v in c.fqan_map.items():
|
||||
for name, v in c.fqan_map.items():
|
||||
# print(k)
|
||||
k = _parse_method_id(k, c)
|
||||
name = activity_split(name)[1]
|
||||
if v.get('parameters'):
|
||||
for pk, pv in v.parameters.items():
|
||||
enums = _add_enum_value(k, pk, pv, enums)
|
||||
enum = get_enum_if_is_enum(name, pk, pv)
|
||||
|
||||
if enum:
|
||||
enums[(name, pk)] = (name, pk, enum, pv)
|
||||
|
||||
return list(enums.values())
|
||||
|
||||
|
||||
|
||||
@@ -461,7 +461,7 @@ def to_rust_type_inner(
|
||||
return wrap_type(rt)
|
||||
try:
|
||||
if is_property_enum(t):
|
||||
x = get_enum_type(schema_name, property_name, t, schemas)
|
||||
x = get_enum_type(schema_name, property_name)
|
||||
return wrap_type(x)
|
||||
# prefer format if present
|
||||
rust_type = RUST_TYPE_MAP[t.get("format", t["type"])]
|
||||
@@ -774,6 +774,7 @@ class Context:
|
||||
rta_map: Dict[str, Any]
|
||||
rtc_map: Dict[str, Any]
|
||||
schemas: Dict[str, Any]
|
||||
enums: List[Tuple[str, str, RustType, Dict[str, Any]]]
|
||||
|
||||
|
||||
# return a newly build context from the given data
|
||||
@@ -899,7 +900,7 @@ def new_context(schemas: Dict[str, Dict[str, Any]], resources: Dict[str, Any]) -
|
||||
|
||||
all_schemas = schemas and build_schema_map() or dict()
|
||||
if not resources:
|
||||
return Context(dict(), dict(), dict(), dict(), all_schemas)
|
||||
return Context(dict(), dict(), dict(), dict(), all_schemas, [])
|
||||
|
||||
rta_map: Dict[str, Any] = {}
|
||||
rtc_map: Dict[str, Any] = {}
|
||||
@@ -914,7 +915,12 @@ def new_context(schemas: Dict[str, Dict[str, Any]], resources: Dict[str, Any]) -
|
||||
# end for each fqan
|
||||
sta_map.update(_sta_map)
|
||||
fqan_map.update(_fqan_map)
|
||||
return Context(sta_map, fqan_map, rta_map, rtc_map, all_schemas)
|
||||
context = Context(sta_map, fqan_map, rta_map, rtc_map, all_schemas, [])
|
||||
|
||||
from .enum_utils import find_enums_in_context
|
||||
context.enums = find_enums_in_context(context)
|
||||
|
||||
return context
|
||||
|
||||
|
||||
def _is_special_version(v):
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
hub_type = hub_type(c.schemas, util.canonical_name())
|
||||
ht_params = hub_type_params_s()
|
||||
|
||||
enums = find_enums_in_context(c)
|
||||
|
||||
default_user_agent = "google-api-rust-client/" + cargo.build_version
|
||||
%>\
|
||||
@@ -20,6 +19,6 @@ use super::*;
|
||||
|
||||
|
||||
|
||||
% for schema_name,property_name,enum_type, e in enums:
|
||||
% for schema_name,property_name,enum_type, e in c.enums:
|
||||
${enum.new(enum_type, e, c)}
|
||||
% endfor
|
||||
|
||||
Reference in New Issue
Block a user