From d61539282c5f5b7f53538a888b5141005d2f10b2 Mon Sep 17 00:00:00 2001 From: Leon van Kammen Date: Fri, 31 Mar 2023 14:47:54 +0200 Subject: [PATCH] refactored Url.hx to URI.hx --- doc/1.0.0/url.md | 42 ------------------------------- doc/RFC.md | 6 +++-- make | 2 +- src/Test.hx | 12 ++++----- src/xrfragment/{Url.hx => URI.hx} | 11 +++----- test/generated/test.js | 9 +++---- test/generated/test.py | 9 +++---- 7 files changed, 23 insertions(+), 68 deletions(-) delete mode 100644 doc/1.0.0/url.md rename src/xrfragment/{Url.hx => URI.hx} (83%) diff --git a/doc/1.0.0/url.md b/doc/1.0.0/url.md deleted file mode 100644 index 1cf77cc..0000000 --- a/doc/1.0.0/url.md +++ /dev/null @@ -1,42 +0,0 @@ - -# Fragment (values) - -| param | type | category | example | -|---------|---------------|-------------------------|------------------| -| pos | 3D vector | HREF navigation/portals | `#pos=1,0,1` or `#pos=foo` | -| prio | int (-10..1) | Asset linking | `#prio=-5` | - - -# Url parser (the gist of it) - -1. fragment URI starts with `#` -1. fragments are split by `&` -1. fragment-values are urlencoded (space becomes `+` using `encodeUriComponent` e.g.) -1. `=` is used to split fragment key/values -1. `|` is used to split multiple/fallback values -1. `,` assumes 1D/2D/3D vector-values like x[,y[,z]] -1. parseFloat(..) and parseInt(..) is applied to vector/float and int values -1. anything else will be treated as string-value -1. incompatible value-types will be dropped / not used - -> the xrfragment specification should stay simple enough -> for anyone to write a parser using either regexes or grammar/lexers -> therefore expressions/comprehensions are not supported (max wildcard/comparison operators for queries e.g.) - -# Value types - -| type | info | format | example | -|------|------|--------|----------------------------------| -|vector| x,y,z| comma-separated | #pos=1,2,3 | -|string| color| FFFFFF (hex) | #fog=5m,FFAACC | -|string| | | #q=-sun | -|int | | [-]x[xxxxx] | #price:>=100 | -|float | | [-]x[.xxxx] (ieee)| #prio=-20 -|array | mixed| \|-separated | #pos=0,0,0|90,0,0 | - -> rule for thumb: type-limitations will piggyback JSON limitations (IEEE floatsize e.g.) - -1. hex colors are detected using regex `/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/` -1. integers are detected using regex `/^[0-9]+$/` -1. floats are detected using regex `/^[0-9]+\.[0-9]+$/` -1. vectors are detected using regex `/[,]/` (but can also be an string referring to an entity-ID in the asset) diff --git a/doc/RFC.md b/doc/RFC.md index c4e9b58..58354d2 100644 --- a/doc/RFC.md +++ b/doc/RFC.md @@ -1,10 +1,12 @@ > version 1.0.0 -date: 2023-03-31T14:39:54+0200 (generated by `./make doc`) +date: 2023-03-31T14:47:04+0200 (generated by `./make doc`) + +example URI: `://domain.com/some3d.asset#pos=1,0,0&prio=-5 # URI parser -> icanhazcode? yes, see [Url.hx](./../src/xrfragment/Url.hx) +> icanhazcode? yes, see [URI.hx](./../src/xrfragment/URI.hx) 1. fragment URI starts with `#` 1. fragments are split by `&` diff --git a/make b/make index 691a335..d91975d 100755 --- a/make +++ b/make @@ -45,7 +45,7 @@ doc(){ { echo "> version $VERSION" echo "\ndate: $(date +"%Y-%m-%dT%H:%M:%S%z") (generated by \`./make doc\`)" - generate src/xrfragment/Url.hx + generate src/xrfragment/URI.hx generate src/xrfragment/Parser.hx } > doc/RFC.md } diff --git a/src/Test.hx b/src/Test.hx index bc1f321..8342e82 100644 --- a/src/Test.hx +++ b/src/Test.hx @@ -1,5 +1,5 @@ import xrfragment.Query; -import xrfragment.Url; +import xrfragment.URI; class Spec { macro public static function load(path : String) { @@ -29,7 +29,7 @@ class Test { var valid:Bool = false; var item:Dynamic = spec[i]; if( item.fn == "query" ) q = new Query(item.data); - if( item.fn == "url" ) res = Url.parse(item.data); + if( item.fn == "url" ) res = URI.parse(item.data); if( item.expect.fn == "test" ) valid = item.expect.out == q.test( item.expect.input[0] ); if( item.expect.fn == "testProperty" ) valid = item.expect.out == q.testProperty( item.expect.input[0], item.expect.input[1] ); if( item.expect.fn == "testPropertyExclude" ) valid = item.expect.out == q.testProperty( item.expect.input[0], item.expect.input[1], true ); @@ -55,10 +55,10 @@ class Test { } static public function testUrl():Void { - var Url = xrfragment.Url; - var uri:String = "http://foo.com?foo=1#bar=flop&a=1,2&b=c|d|1,2,3"; - trace(uri); - trace( Url.parse(uri) ); + var Uri = xrfragment.URI; + var url:String = "http://foo.com?foo=1#bar=flop&a=1,2&b=c|d|1,2,3"; + trace(url); + trace( Uri.parse(url) ); } static public function testQuery():Void { diff --git a/src/xrfragment/Url.hx b/src/xrfragment/URI.hx similarity index 83% rename from src/xrfragment/Url.hx rename to src/xrfragment/URI.hx index 81e54f2..fe8e6e4 100644 --- a/src/xrfragment/Url.hx +++ b/src/xrfragment/URI.hx @@ -4,14 +4,12 @@ import xrfragment.Parser; @:expose // <- makes the class reachable from plain JavaScript @:keep // <- avoids accidental removal by dead code elimination - -class Url { - - public static var error:String = ""; - + // + // example URI: `://domain.com/some3d.asset#pos=1,0,0&prio=-5 +class URI { @:keep // # URI parser public static function parse(qs:String):haxe.DynamicAccess { // - var fragment:Array = qs.split("#"); // > icanhazcode? yes, see [Url.hx](./../src/xrfragment/Url.hx) + var fragment:Array = qs.split("#"); // > icanhazcode? yes, see [URI.hx](./../src/xrfragment/URI.hx) var splitArray:Array = fragment[1].split('&'); // var resultMap:haxe.DynamicAccess = {}; // 1. fragment URI starts with `#` for (i in 0...splitArray.length) { // 1. fragments are split by `&` @@ -27,5 +25,4 @@ class Url { } return resultMap; } - } diff --git a/test/generated/test.js b/test/generated/test.js index d11291c..0c73d26 100644 --- a/test/generated/test.js +++ b/test/generated/test.js @@ -151,7 +151,7 @@ Test.test = function(spec) { q = new xrfragment_Query(item.data); } if(item.fn == "url") { - res = xrfragment_Url.parse(item.data); + res = xrfragment_URI.parse(item.data); } if(item.expect.fn == "test") { valid = item.expect.out == q.test(item.expect.input[0]); @@ -517,9 +517,9 @@ xrfragment_Query.prototype = { return qualify > 0; } }; -var xrfragment_Url = $hx_exports["xrfragment"]["Url"] = function() { }; -xrfragment_Url.__name__ = true; -xrfragment_Url.parse = function(qs) { +var xrfragment_URI = $hx_exports["xrfragment"]["URI"] = function() { }; +xrfragment_URI.__name__ = true; +xrfragment_URI.parse = function(qs) { var fragment = qs.split("#"); var splitArray = fragment[1].split("&"); var resultMap = { }; @@ -579,7 +579,6 @@ var xrfragment_Query_ok = $hx_exports["xrfragment"]["Query"]["ok"] = } } ; -xrfragment_Url.error = ""; Test.main(); })({}); var xrfragment = $hx_exports["xrfragment"]; diff --git a/test/generated/test.py b/test/generated/test.py index 7680be8..1a06412 100644 --- a/test/generated/test.py +++ b/test/generated/test.py @@ -400,7 +400,7 @@ class Test: if (Reflect.field(item,"fn") == "query"): q = xrfragment_Query(Reflect.field(item,"data")) if (Reflect.field(item,"fn") == "url"): - res = xrfragment_Url.parse(Reflect.field(item,"data")) + res = xrfragment_URI.parse(Reflect.field(item,"data")) if (Reflect.field(Reflect.field(item,"expect"),"fn") == "test"): valid = (Reflect.field(Reflect.field(item,"expect"),"out") == q.test(HxOverrides.arrayGet(Reflect.field(Reflect.field(item,"expect"),"input"), 0))) if (Reflect.field(Reflect.field(item,"expect"),"fn") == "testProperty"): @@ -1622,10 +1622,10 @@ class xrfragment_Query: -class xrfragment_Url: - _hx_class_name = "xrfragment.Url" +class xrfragment_URI: + _hx_class_name = "xrfragment.URI" __slots__ = () - _hx_statics = ["error", "parse"] + _hx_statics = ["parse"] @staticmethod def parse(qs): @@ -1661,6 +1661,5 @@ xrfragment_Type.isColor = EReg("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$","") xrfragment_Type.isInt = EReg("^[0-9]+$","") xrfragment_Type.isFloat = EReg("^[0-9]+\\.[0-9]+$","") xrfragment_Type.isVector = EReg("([,]+|\\w)","") -xrfragment_Url.error = "" Test.main()