Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement basic Lua output support #1745

Merged
merged 22 commits into from Aug 11, 2023
Merged

Implement basic Lua output support #1745

merged 22 commits into from Aug 11, 2023

Conversation

Zash
Copy link
Contributor

@Zash Zash commented Jul 30, 2023

First attempt at #1700

Supports basic serialization to Lua tables:

$ ./yq -n  -olua '.hello="\"world\"",.foo=["bar","baz",3,1.3]' 
return {["hello"]="\"world\"";["foo"]={"bar","baz",3,1.3,};};

No indentation or pretty printing yet, likely next steps.

Feedback appreciated, hence the Draft PR. (Haven't written Go in a couple of years so be gentle.)

@Zash Zash force-pushed the luaout branch 2 times, most recently from 3dcf9b9 to 371e60d Compare July 31, 2023 06:48
@mikefarah
Copy link
Owner

Nice, looks great so far! You'll need to add some unit tests (which also generate docs) - see toml_test.go or xml_test.go for reference.

@Zash Zash force-pushed the luaout branch 8 times, most recently from 4470030 to 9629a6a Compare August 5, 2023 13:29
Zash added 17 commits August 5, 2023 17:08
Ref mikefarah#1700

Basic but working serialization to Lua tables.
Started with a minimum of replacements, this should be more complete,
tho not all substitutions are strictly required in Lua.
String keys that satisfy the requirements for variable names can be used
as keys without quotes in tables.
Keywords are not valid as unquoted keys, thus must be quoted
Generally safer and simpler to not do it.
--lua-suffix='});^M' didn't work, so taking this approach instead
Could add some context tracking in the future to allow rejecting nil in
a table key context.
Boilerplate mostly copied from toml_test.go
Mostly just for the boilerplate
Lua doesn't have the 0oNNN syntax for octal integers, only decimal and
hexadecimal, hence those can be passed trough as is while octal needs
special treatment.
@Zash Zash force-pushed the luaout branch 2 times, most recently from 2870675 to 38c945a Compare August 5, 2023 15:14
@Zash
Copy link
Contributor Author

Zash commented Aug 5, 2023

Okay, I think it does all I want it to now.

  • Indentation
  • Tests (uncertain of coverage)
  • Documentation (generated)
  • Optional unquoted string keys
  • Optional global mode
  • Line comments (but not head or foot comments)
yq$ ./yq -n -o=lua '.hello=["world","you"]'
return {
	["hello"] = {
		"world",
		"you",
	};
};
yq$ ./yq -n -o=lua --lua-unquoted '.hello=["world","you"]'
return {
	hello = {
		"world",
		"you",
	};
};
yq$ ./yq -n -o=lua --lua-globals '.hello=["world","you"]'
hello = {
	"world",
	"you",
};

@Zash Zash marked this pull request as ready for review August 5, 2023 16:01
@mikefarah
Copy link
Owner

Wow great work!

@mikefarah mikefarah merged commit d302d75 into mikefarah:master Aug 11, 2023
3 checks passed
tearmoon pushed a commit to taamarin/yq that referenced this pull request Aug 11, 2023
* Implement basic Lua output support

Ref mikefarah#1700

Basic but working serialization to Lua tables.

* Escape larger set of characters in Lua output

Started with a minimum of replacements, this should be more complete,
tho not all substitutions are strictly required in Lua.

* Print simple keys unquoted in Lua output

String keys that satisfy the requirements for variable names can be used
as keys without quotes in tables.

* Quote Lua keywords in table keys

Keywords are not valid as unquoted keys, thus must be quoted

* Make output of unquoted Lua table keys optional

Generally safer and simpler to not do it.

* Hook up settings for Lua output

* Allow special characters in Lua prefix and suffix

--lua-suffix='});^M' didn't work, so taking this approach instead

* Panic on unhandled YAML Kind in Lua encoder

* Handle YAML case varied booleans in Lua encoder

* Handle special-case numbers in Lua encoder

* Reject unhandled scalar Tags in Lua encoder

* Add note about how Lua nil is unsuitable as table key

Could add some context tracking in the future to allow rejecting nil in
a table key context.

* Return error instead of panic in Lua encoder

* Add initial test for Lua encoder

Boilerplate mostly copied from toml_test.go

* Additional Lua output tests

* Generate Lua encoder documentation

Mostly just for the boilerplate

* Convert octal for Lua output

Lua doesn't have the 0oNNN syntax for octal integers, only decimal and
hexadecimal, hence those can be passed trough as is while octal needs
special treatment.

* Implement indentation in in Lua output

* Respect string Style in Lua encoder

Lua has 'single', "double" and [[ long ]] strings.

* Expand Lua examples

* Output line comments in Lua output

* Implement Lua globals output mode

(cherry picked from commit d302d75)
tearmoon pushed a commit to taamarin/yq that referenced this pull request Aug 11, 2023
* Implement basic Lua output support

Ref mikefarah#1700

Basic but working serialization to Lua tables.

* Escape larger set of characters in Lua output

Started with a minimum of replacements, this should be more complete,
tho not all substitutions are strictly required in Lua.

* Print simple keys unquoted in Lua output

String keys that satisfy the requirements for variable names can be used
as keys without quotes in tables.

* Quote Lua keywords in table keys

Keywords are not valid as unquoted keys, thus must be quoted

* Make output of unquoted Lua table keys optional

Generally safer and simpler to not do it.

* Hook up settings for Lua output

* Allow special characters in Lua prefix and suffix

--lua-suffix='});^M' didn't work, so taking this approach instead

* Panic on unhandled YAML Kind in Lua encoder

* Handle YAML case varied booleans in Lua encoder

* Handle special-case numbers in Lua encoder

* Reject unhandled scalar Tags in Lua encoder

* Add note about how Lua nil is unsuitable as table key

Could add some context tracking in the future to allow rejecting nil in
a table key context.

* Return error instead of panic in Lua encoder

* Add initial test for Lua encoder

Boilerplate mostly copied from toml_test.go

* Additional Lua output tests

* Generate Lua encoder documentation

Mostly just for the boilerplate

* Convert octal for Lua output

Lua doesn't have the 0oNNN syntax for octal integers, only decimal and
hexadecimal, hence those can be passed trough as is while octal needs
special treatment.

* Implement indentation in in Lua output

* Respect string Style in Lua encoder

Lua has 'single', "double" and [[ long ]] strings.

* Expand Lua examples

* Output line comments in Lua output

* Implement Lua globals output mode

(cherry picked from commit d302d75)
tearmoon pushed a commit to taamarin/yq that referenced this pull request Aug 11, 2023
* Implement basic Lua output support

Ref mikefarah#1700

Basic but working serialization to Lua tables.

* Escape larger set of characters in Lua output

Started with a minimum of replacements, this should be more complete,
tho not all substitutions are strictly required in Lua.

* Print simple keys unquoted in Lua output

String keys that satisfy the requirements for variable names can be used
as keys without quotes in tables.

* Quote Lua keywords in table keys

Keywords are not valid as unquoted keys, thus must be quoted

* Make output of unquoted Lua table keys optional

Generally safer and simpler to not do it.

* Hook up settings for Lua output

* Allow special characters in Lua prefix and suffix

--lua-suffix='});^M' didn't work, so taking this approach instead

* Panic on unhandled YAML Kind in Lua encoder

* Handle YAML case varied booleans in Lua encoder

* Handle special-case numbers in Lua encoder

* Reject unhandled scalar Tags in Lua encoder

* Add note about how Lua nil is unsuitable as table key

Could add some context tracking in the future to allow rejecting nil in
a table key context.

* Return error instead of panic in Lua encoder

* Add initial test for Lua encoder

Boilerplate mostly copied from toml_test.go

* Additional Lua output tests

* Generate Lua encoder documentation

Mostly just for the boilerplate

* Convert octal for Lua output

Lua doesn't have the 0oNNN syntax for octal integers, only decimal and
hexadecimal, hence those can be passed trough as is while octal needs
special treatment.

* Implement indentation in in Lua output

* Respect string Style in Lua encoder

Lua has 'single', "double" and [[ long ]] strings.

* Expand Lua examples

* Output line comments in Lua output

* Implement Lua globals output mode

(cherry picked from commit d302d75)
@Zash Zash deleted the luaout branch August 11, 2023 19:25
@Zash Zash mentioned this pull request Sep 30, 2023
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.

None yet

2 participants