-
-
Notifications
You must be signed in to change notification settings - Fork 114
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
Issues with custom tags #457
Comments
You're right; the link should've pointed at the file you found and there were a couple of missing exports. import { createNode, createPair } from 'yaml/util' I've released v2.3.0-1 that should make the necessary things public; it's available on npm as |
Fantastic, thanks! I'll make another pass at this soon and report back if there are any other blockers. (Stretch goal is to send a doc PR adding a complex object/map example to the existing string-type example.) |
Got this pretty much working, here's an example of a type for a null-prototype object, about the simplest non-scalar type tap-yaml has: https://github.com/tapjs/tap-yaml/blob/isaacs/yaml2/lib/types/null-object.js This bit here feels like it could be more elegant somehow: https://github.com/tapjs/tap-yaml/blob/isaacs/yaml2/lib/types/null-object.js#L15-L29 It's kind of surprising that I'm getting a YAMLMap in the Same with |
@eemeli So here's what I'm thinking would make this a whole lot nicer, with relatively little change to the inner workings. First, move the existing logic in Then, in diff --git a/src/doc/createNode.ts b/src/doc/createNode.ts
index d6e7241..fbea500 100644
--- a/src/doc/createNode.ts
+++ b/src/doc/createNode.ts
@@ -97,7 +97,9 @@ export function createNode(
delete ctx.onTagObj
}
- const node = tagObj?.createNode
+ const node = typeof tagObj?.nodeClass?.createNode === 'function'
+ ? tagObj?.nodeClass?.createNode(ctx.schema, value, ctx)
+ : tagObj?.createNode
? tagObj.createNode(ctx.schema, value, ctx)
: new Scalar(value)
if (tagName) node.tag = tagName That seems pretty low impact. Would probably want to move the other collection tag definitions to use a similar pattern, but very straightforward. For the other part, in What I'd like to do with my custom tag is to say that it must be a So, it seems to me that the thing to do here is to move the That's a somewhat bigger change, so I wanted to check before diving into it. (I'm sure you know all this, just reporting what I found in digging so that if there's something obvious I missed, it'll stand out.) But with those two, the total implementation for a custom const { YAMLMap } = require('yaml')
const tag = '!nullobject'
class YAMLNullObject extends YAMLMap {
get tag() { return tag }
set tag(_) {}
toJSON(_, ctx) {
const obj = super.toJSON(_, {...ctx, mapAsMap: false}, Object)
return Object.assign(Object.create(null), obj)
}
}
module.exports = {
tag,
collection: 'map',
nodeClass: YAMLNullObject,
identify: v => !!(
typeof v === 'object' &&
v &&
!Object.getPrototypeOf(v)
)
} or even cuter: import { YAMLMap } from 'yaml'
const tag = '!nullobject'
export default class YAMLNullObject extends YAMLMap {
get tag() { return tag }
set tag(_) {}
toJSON(_, ctx) {
const obj = super.toJSON(_, {...ctx, mapAsMap: false}, Object)
return Object.assign(Object.create(null), obj)
}
static get tag() { return tag }
static get collection() { return 'map' }
static identify(v) {
return !!(
typeof v === 'object' &&
v &&
!Object.getPrototypeOf(v)
)
}
static get nodeClass () { return YAMLNullObject }
} |
Sorry for the delay; needed to think about your proposals and was afk for Easter. I rather like the idea of moving the currently hard-to-reach For backwards compatibility, I think Would you be interested in submitting a PR with these changes? I've also been thinking that it might make sense to put together a separate package of useful custom tags; e.g. null objects and regexps could be useful to many users. Would you be interested in contributing to such a collection? |
It's taken me a year to even start looking at yaml2 for node-tap, so I think you're good 😂
Sure, thanks for the direction. That all sounds very reasonable. Re the bikeshed,
Yes! That's essentially all that |
Heh, sometimes fixing what ain't broken does take quite a bit of energy.
Yeah, that was my thinking as well. Of course in this case there are a couple of arguments instead of just one, but
Excellent! I'll ping you if/once I get something like that off the ground. |
The TypedArray.from() methods all take multiple args, you can pass in a map function and |
Is there a build step or something to get the yaml/json fixtures for tests? I'm getting this failure:
|
The JSON and YAML test suites use data from git submodules, so run this in the repo root to set them up:
|
@eemeli If you have any time to provide eyes on #467, it'd be much appreciated. It's just a first pass, not sure if it's the most elegant way to go about it, but seems to work for my custom tags as described in this issue (albeit without checking if the custom tag gets the right kind of collection), and passes existing tests. |
Describe the bug
The docs contain a link to https://github.com/eemeli/yaml/blob/master/src/tags/yaml-1.1/set.js in the custom tags section.
To Reproduce
Self explanatory
Expected behaviour
Should link to the implementation of the
!!set
custom tag. (I'm guessing it's this?)Versions (please complete the following information):
yaml
: 2.2Additional context
Assuming that https://github.com/eemeli/yaml/blob/main/src/schema/yaml-1.1/set.ts is the correct link, there's still (at least) a documentation shortcoming in this section.
It is provided as an example of how to implement a complex custom type, but looking at that code, it appears to be relying on
createPair
,Pair
,createNode
, and other functions that are not exported by the library. ThemapTag
is exported, but attempting to just hijack its createNode method fails withTypeError: schema.createPair is not a function
It feels like something was supposed to be exported (or documented?) when
parseMap
was removed, but I'm not finding it.The text was updated successfully, but these errors were encountered: