Antoine Goutenoir 959cf1c3c9
[GdScript] Templates for GdScript (Godot 4) (#19267)
* feat(gdscript): sketch implementation of gdscript target language

This does not really work yet, but it's a start.
Results are not denormalized, no support for enums nor datetimes,
and thousands of other features are missing.

I still don't know how we are going to denormalize JSON+LD
without writing a whole GDScript lib for it…

* feat: add an exhaustive list of keywords reserved in GDScript

I've also provided the small python script I used to generate the list.

* refacto(gdscript): start using partials in templates

Whilst I'm racking my brains trying to figure out integration testing…

* test(gdscript): prepare a demo and integration testing

* fix(gdscript): do not use subclasses, use plain POGO

(plain ol' godot object)

One: I don't know how they work under-the-hood.
Two: I'm very confused over-the-hood.
Tri: We do not need them.

* refacto(gdscript): move demo files to their own directory

I know I'm making a lot of commits for not much,
but now I'm opening the sample files with Godot as well,
and doing unholy things with filesystems,
so I'm not taking any chances.

It's all going to be squashed anyway.  :)

* fix(gdscript): sample as a Godot project

It works !  I can now write integration tests in GDScript.
The real work starts now.

/spend 25h

* feat(gdscript): serialize and send body params

The test suite is now past its first hurdle, the 415 HTTP status code,
and went straight into an unexpected error 500.

I suspect the server does not like me trying to set the pet id at 0,
because that's what we're trying to do right now.

Godot is crashing a lot, mostly because I don't know how to make Callable.NOOP
and my current solution hints at optional on_success and on_failure,
yet if we omit them the engine will ragequit.

* feat(gdscript): check request body for required yet missing properties

Now we'll get a nice error when we forget to set a required property.

The demo is now able to:
- connect
- create a user
- login as that user
- create a pet

* feat(gdscript): namespace core classes as well with apiPrefixName

This makes our usage of `class_name` a little more acceptable.

* feat(gdscript): support prefixes and suffixes for class names

This will crutch namespacing well enough for most uses.

* feat(gdscript): handle enums, naively

* feat(gdscript): support basic API endpoint param constraints

- minLength
- maxLength
- minItems
- maxItems
- minimum
- maximum
- pattern (no flags)

* feat(gdscript): handle header params and header customization

We also support serializing to application/x-www-form-urlencoded now.

Next up: DateTimes !

* feat(gdscript): handle Date and DateTime like Strings

There's no timezone support in Godot for DateTimes.

* feat(gdscript): support plain text responses

* feat(gdscript): support collections of models

Those are Arrays, not custom collection objects.

* feat(gdscript): configure default host from OAS

* feat(gdscript): some documentation and better config

We don't need no factories nor singletons ; config is enough.

* docs(gdscript): document usage a little

* feat(gdscript): add more reserved words, skip jsonld models and configure features

We can now generate a client for an OAS server running ApiPlatform (PHP).

* feat(gdscript): improve logging with a configurable log level

* feat(gdscript): add support for Basic Bearer and Header ApiKey

(but I can't find the `description` template handler)

* fix(gdscript)

Too late to amend >.<

* fix(gdscript)

dangsarnit

* chore(gdscript): clean up a sprint artifact

* fix: don't forget the HTTP error code when relevant

* feat: use Resource as base class for models

* fix. Default string values now with "quotes"

* temporary remove settings as godot api have changed

* kick ci

* docs: review gdscript java class

* feat: support for TLS, some refacto, some review

There's still a lot of holes, TODOs and FIXMEs.

* feat: experimental support of Request inline objects

The inline response objects are still not supported.

* feat(gdscript): support inline request and response objects

* chore(gdscript): review the templates

* fix(gdscript): unexpected nulls in default values

{{#if defaultValue}} evaluates to true for null if we call super here.

* refacto(gdscript): replace "bee" prefix by "bzz", use a constructor

Now we pass the config and the client via the constructor.
This reduces the area of the public surface a bit, for the better I think.
This commit also cleans up the class name shenanigans.

* fix(gdscript): add missing file

* test(gdscript): refactor the test project to use the generated lib as addon

Since there is no singleton in the generated client, the addon need not be enabled in the project configuration to be usable.

The --headless mode is broken for now, as things changed in Godot 4 since the beta.

* docs(gdscript): document petstore server ADR

* test(gdscript): add GUT and an integration test

We used the latest stable GUT, but a feature we're going to need was merged today, so we'll need to update it either to master or to the next release at some point.

* refacto(gdscript, breaking): use an ApiResponse object in success callbacks

/spent 6d since the beginning

* test(gdscript): update integration tests

/spend 2h

* docs(gdscript): explain the new ApiResponse

Also moving core templates to their own subdir, for clarity.

/spend 10m

* chore(gdscript): review, document, clean up

/spend 2h

* test(gdscript): test the delete operation as well

/spend 7m

* feat(gdscript): update GUT and exit with appropriate code

/spend 2h

* docs(gdscript): add Gdscript's PI

Hire me while I'm available !  :D
I'd rather code than make a CV.

* feat(gdscript): support reserved keywords

Also adding some more assertions,
and using our own OAS file now.

/spend 3h

* refacto(gdscript): use "base" instead of "bee"

/spend 1h

* feat(gdscript): improve descriptions

/spend 1h

* fix(gdscript): await before polling

Contributed by @jchu231

* docs(gdscript): review the template files

* docs(gdscript): review and generate docs

---------

Co-authored-by: Bagrat <b.saatsazov@gmail.com>
2024-11-17 10:48:19 +08:00

233 lines
6.7 KiB
GDScript

# -------------
# returns{} and parameters {} have the followin structure
# -------------
# {
# inst_id_or_path1:{
# method_name1: [StubParams, StubParams],
# method_name2: [StubParams, StubParams]
# },
# inst_id_or_path2:{
# method_name1: [StubParams, StubParams],
# method_name2: [StubParams, StubParams]
# }
# }
var returns = {}
var _utils = load('res://addons/gut/utils.gd').get_instance()
var _lgr = _utils.get_logger()
var _strutils = _utils.Strutils.new()
var _class_db_name_hash = {}
func _init():
_class_db_name_hash = _make_crazy_dynamic_over_engineered_class_db_hash()
# So, I couldn't figure out how to get to a reference for a GDNative Class
# using a string. ClassDB has all thier names...so I made a hash using those
# names and the classes. Then I dynmaically make a script that has that as
# the source and grab the hash out of it and return it. Super Rube Golbergery,
# but tons of fun.
func _make_crazy_dynamic_over_engineered_class_db_hash():
var text = "var all_the_classes = {\n"
for classname in ClassDB.get_class_list():
if(ClassDB.can_instantiate(classname)):
text += str('"', classname, '": ', classname, ", \n")
else:
text += str('# ', classname, "\n")
text += "}"
var inst = _utils.create_script_from_source(text).new()
return inst.all_the_classes
func _find_matches(obj, method):
var matches = null
var last_not_null_parent = null
# Search for what is passed in first. This could be a class or an instance.
# We want to find the instance before we find the class. If we do not have
# an entry for the instance then see if we have an entry for the class.
if(returns.has(obj) and returns[obj].has(method)):
matches = returns[obj][method]
elif(_utils.is_instance(obj)):
var parent = obj.get_script()
var found = false
while(parent != null and !found):
found = returns.has(parent)
if(!found):
last_not_null_parent = parent
parent = parent.get_base_script()
# Could not find the script so check to see if a native class of this
# type was stubbed.
if(!found):
var base_type = last_not_null_parent.get_instance_base_type()
if(_class_db_name_hash.has(base_type)):
parent = _class_db_name_hash[base_type]
found = returns.has(parent)
if(found and returns[parent].has(method)):
matches = returns[parent][method]
return matches
# Searches returns for an entry that matches the instance or the class that
# passed in obj is.
#
# obj can be an instance, class, or a path.
func _find_stub(obj, method, parameters=null, find_overloads=false):
var to_return = null
var matches = _find_matches(obj, method)
if(matches == null):
return null
var param_match = null
var null_match = null
var overload_match = null
for i in range(matches.size()):
var cur_stub = matches[i]
if(cur_stub.parameters == parameters):
param_match = cur_stub
if(cur_stub.parameters == null and !cur_stub.is_param_override_only()):
null_match = cur_stub
if(cur_stub.has_param_override()):
if(overload_match == null || overload_match.is_script_default):
overload_match = cur_stub
if(find_overloads and overload_match != null):
to_return = overload_match
# We have matching parameter values so return the stub value for that
elif(param_match != null):
to_return = param_match
# We found a case where the parameters were not specified so return
# parameters for that. Only do this if the null match is not *just*
# a paramerter override stub.
elif(null_match != null):
to_return = null_match
return to_return
# ##############
# Public
# ##############
func add_stub(stub_params):
stub_params._lgr = _lgr
var key = stub_params.stub_target
if(!returns.has(key)):
returns[key] = {}
if(!returns[key].has(stub_params.stub_method)):
returns[key][stub_params.stub_method] = []
returns[key][stub_params.stub_method].append(stub_params)
# Gets a stubbed return value for the object and method passed in. If the
# instance was stubbed it will use that, otherwise it will use the path and
# subpath of the object to try to find a value.
#
# It will also use the optional list of parameter values to find a value. If
# the object was stubbed with no parameters than any parameters will match.
# If it was stubbed with specific parameter values then it will try to match.
# If the parameters do not match BUT there was also an empty parameter list stub
# then it will return those.
# If it cannot find anything that matches then null is returned.for
#
# Parameters
# obj: this should be an instance of a doubled object.
# method: the method called
# parameters: optional array of parameter vales to find a return value for.
func get_return(obj, method, parameters=null):
var stub_info = _find_stub(obj, method, parameters)
if(stub_info != null):
return stub_info.return_val
else:
_lgr.warn(str('Call to [', method, '] was not stubbed for the supplied parameters ', parameters, '. Null was returned.'))
return null
func should_call_super(obj, method, parameters=null):
if(_utils.non_super_methods.has(method)):
return false
var stub_info = _find_stub(obj, method, parameters)
var is_partial = false
if(typeof(obj) != TYPE_STRING): # some stubber tests test with strings
is_partial = obj.__gutdbl.is_partial
var should = is_partial
if(stub_info != null):
should = stub_info.call_super
elif(!is_partial):
# this log message is here because of how the generated doubled scripts
# are structured. With this log msg here, you will only see one
# "unstubbed" info instead of multiple.
_lgr.info('Unstubbed call to ' + method + '::' + _strutils.type2str(obj))
should = false
return should
func get_parameter_count(obj, method):
var to_return = null
var stub_info = _find_stub(obj, method, null, true)
if(stub_info != null and stub_info.has_param_override()):
to_return = stub_info.parameter_count
return to_return
func get_default_value(obj, method, p_index):
var to_return = null
var stub_info = _find_stub(obj, method, null, true)
if(stub_info != null and
stub_info.parameter_defaults != null and
stub_info.parameter_defaults.size() > p_index):
to_return = stub_info.parameter_defaults[p_index]
return to_return
func clear():
returns.clear()
func get_logger():
return _lgr
func set_logger(logger):
_lgr = logger
func to_s():
var text = ''
for thing in returns:
text += str("-- ", thing, " --\n")
for method in returns[thing]:
text += str("\t", method, "\n")
for i in range(returns[thing][method].size()):
text += "\t\t" + returns[thing][method][i].to_s() + "\n"
if(text == ''):
text = 'Stubber is empty';
return text
func stub_defaults_from_meta(target, method_meta):
var params = _utils.StubParams.new(target, method_meta)
params.is_script_default = true
add_stub(params)