Skip to content

Commit 25e2130

Browse files
committed
yaml: cleanup API and docs
- Rename YamlObj.content to val (leave old one deprecated) - Use covaraince in YamlObj.val method subclasses - Make YamlObj.make internal, hide fields - Make more stuff nodoc
1 parent c5fb936 commit 25e2130

File tree

5 files changed

+145
-120
lines changed

5 files changed

+145
-120
lines changed

src/yaml/fan/YamlObj.fan

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,29 @@ abstract const class YamlObj
2929

3030
** The node's tag. Either a specific tag (e.g. 'tag:yaml.org,2002:str')
3131
** or the non-specific tag '?'.
32-
const Str tag := "?"
32+
Str tag() { tagRef }
33+
internal const Str tagRef
3334

34-
** The node's content. [YamlScalars]`yaml::YamlScalar` always have
35+
** The node's content value. [YamlScalars]`yaml::YamlScalar` always have
3536
** content of type 'Str', [YamlLists]`yaml::YamlList` with content
3637
** type 'YamlObj[]', and [YamlMaps]`yaml::YamlMap` with 'YamlObj:YamlObj'.
37-
const Obj content := 0 //the "?" and 0 are placeholders
38+
virtual Obj val() { valRef }
39+
internal const Obj valRef
40+
41+
@Deprecated { msg = "Use val" }
42+
@NoDoc Obj content() { valRef }
3843

3944
** The text location from which this node was parsed.
40-
const FileLoc loc := FileLoc.unknown
45+
FileLoc loc() { locRef }
46+
internal const FileLoc locRef
47+
48+
** Internal make
49+
internal new make(Obj val, Str tag, FileLoc loc)
50+
{
51+
this.valRef = val
52+
this.tagRef = tag
53+
this.locRef = loc
54+
}
4155

4256
//////////////////////////////////////////////////////////////////////////
4357
// Public methods
@@ -64,11 +78,11 @@ abstract const class YamlObj
6478
{
6579
this.typeof == that?.typeof &&
6680
this.tag == (that as YamlObj).tag &&
67-
this.content == (that as YamlObj).content
81+
this.val == (that as YamlObj).val
6882
}
6983

7084
** Hash is based on tag and content
71-
override Int hash() { 31 * tag.hash + content.hash }
85+
override Int hash() { 31 * tag.hash + val.hash }
7286

7387
** Returns 'write' written into a string.
7488
override Str toStr()
@@ -97,21 +111,26 @@ const class YamlScalar : YamlObj
97111
{
98112
// content : Str
99113

100-
** Creates a YamlScalar with the string 's' as content,
114+
** Creates a YamlScalar with the string 'val' as content,
101115
** found at location 'loc', with 'tag' as its tag.
102-
new make(Str s, Str tag := "?", FileLoc loc := FileLoc.unknown)
116+
new make(Str val, Str tag := "?", FileLoc loc := FileLoc.unknown)
117+
: super(val, normTag(tag), loc)
103118
{
104-
this.content = s
105-
this.loc = loc
119+
}
106120

107-
if (tag == "!") this.tag = "tag:yaml.org,2002:str"
108-
else if (tag != "") this.tag = tag
109-
else this.tag = "?"
121+
private static Str normTag(Str tag)
122+
{
123+
if (tag == "!") return "tag:yaml.org,2002:str"
124+
if (tag != "") return tag
125+
return "?"
110126
}
111127

112128
** Convenience for creating a YamlScalar with an explicit file location but
113129
** implicit tag.
114-
new makeLoc(Str s, FileLoc loc) : this.make(s, "?", loc) {}
130+
@NoDoc new makeLoc(Str s, FileLoc loc) : this.make(s, "?", loc) {}
131+
132+
** Content value as a string
133+
override Str val() { valRef }
115134

116135
override internal Void writeInd(OutStream out, Int first, Int next := first)
117136
{
@@ -121,7 +140,7 @@ const class YamlScalar : YamlObj
121140
// Plain scalar
122141
{
123142
//cover when a plain scalar contains '\n'
124-
out.writeChars(Regex("\\n(?=.)").matcher(content).replaceAll("\n\n" + (" " * next)) + "\n")
143+
out.writeChars(Regex("\\n(?=.)").matcher(val).replaceAll("\n\n" + (" " * next)) + "\n")
125144
}
126145
else
127146
// Non-plain - use quotation marks & escape chars
@@ -131,7 +150,7 @@ const class YamlScalar : YamlObj
131150

132151
out.writeChar('"')
133152

134-
(content as Str).each |c|
153+
(val as Str).each |c|
135154
{
136155
// Escape chars
137156
switch(c)
@@ -182,28 +201,30 @@ const class YamlList : YamlObj
182201
{
183202
// content : YamlObj[]
184203

185-
** Creates a YamlList with the list 's' as content,
204+
** Creates a YamlList with the list 'val' as content,
186205
** found at location 'loc', with 'tag' as its tag.
187-
new make(YamlObj[] s, Str tag := "!", FileLoc loc := FileLoc.unknown)
206+
new make(YamlObj[] val, Str tag := "!", FileLoc loc := FileLoc.unknown)
207+
: super(val, normTag(tag), loc)
188208
{
189-
this.content = s
190-
this.loc = loc
209+
}
191210

192-
if (tag == "!" || tag == "") this.tag = "tag:yaml.org,2002:seq"
193-
else this.tag = tag
211+
private static Str normTag(Str tag)
212+
{
213+
if (tag == "!" || tag == "") return "tag:yaml.org,2002:seq"
214+
return tag
194215
}
195216

196217
** Convenience for creating a YamlList with an explicit file location but
197218
** implicit tag.
198-
new makeLoc(YamlObj[] s, FileLoc loc) : this.make(s, "!", loc) {}
219+
@NoDoc new makeLoc(YamlObj[] s, FileLoc loc) : this.make(s, "!", loc) {}
199220

200-
** Content as a list
201-
YamlObj[] list() { content }
221+
** Content value as a list
222+
override YamlObj[] val() { valRef }
202223

203224
** Iterate the list items
204225
Void each(|YamlObj| f)
205226
{
206-
((YamlObj[])content).each(f)
227+
((YamlObj[])val).each(f)
207228
}
208229

209230
override internal Void writeInd(OutStream out, Int first, Int next := first)
@@ -215,7 +236,7 @@ const class YamlList : YamlObj
215236
// normal list
216237
else
217238
{
218-
contList := content as YamlObj[]
239+
contList := val as YamlObj[]
219240
isEmpty := contList.size == 0
220241
isTagged := tag != "?" && tag != "tag:yaml.org,2002:seq"
221242

@@ -237,7 +258,7 @@ const class YamlList : YamlObj
237258
** Writes the content as a stream of YAML documents instead of a list
238259
private Void writeStream(OutStream out)
239260
{
240-
contList := content as YamlObj[]
261+
contList := val as YamlObj[]
241262

242263
if (contList.size != 0)
243264
out.writeChars("%YAML 1.2\n")
@@ -267,27 +288,29 @@ const class YamlMap : YamlObj
267288
{
268289
// content : [YamlObj:YamlObj]
269290

270-
** Creates a YamlMap with the map 's' as content,
291+
** Creates a YamlMap with the map 'val' as content,
271292
** found at location 'loc', with 'tag' as its tag.
272-
new make([YamlObj:YamlObj] s, Str tag := "!", FileLoc loc := FileLoc.unknown)
293+
new make([YamlObj:YamlObj] val, Str tag := "!", FileLoc loc := FileLoc.unknown)
294+
: super(val, normTag(tag), loc)
273295
{
274-
this.content = s
275-
this.loc = loc
296+
}
276297

277-
if (tag == "!" || tag == "") this.tag = "tag:yaml.org,2002:map"
278-
else this.tag = tag
298+
private static Str normTag(Str tag)
299+
{
300+
if (tag == "!" || tag == "") return "tag:yaml.org,2002:map"
301+
return tag
279302
}
280303

281304
** Convenience for creating a YamlMap with an explicit file location but
282305
** implicit tag.
283-
new makeLoc([YamlObj:YamlObj] s, FileLoc loc) : this.make(s, "!", loc) {}
306+
@NoDoc new makeLoc([YamlObj:YamlObj] s, FileLoc loc) : this.make(s, "!", loc) {}
284307

285-
** Content as a map
286-
[YamlObj:YamlObj] map() { content }
308+
** Content value as a map
309+
override [YamlObj:YamlObj] val() { valRef }
287310

288311
override internal Void writeInd(OutStream out, Int first, Int next := first)
289312
{
290-
contMap := content as YamlObj:YamlObj
313+
contMap := val as YamlObj:YamlObj
291314
isEmpty := contMap.keys.size == 0
292315
isTagged := tag != "?" && tag != "tag:yaml.org,2002:map"
293316

@@ -307,14 +330,14 @@ const class YamlMap : YamlObj
307330
kStr := buf.toStr[0..-2] //strip ending '\n'
308331

309332
// Key fits on single line
310-
if ((k.typeof == YamlScalar# || k.content == YamlObj[,] || k.content == [YamlObj:YamlObj][:]) &&
333+
if ((k.typeof == YamlScalar# || k.val == YamlObj[,] || k.val == [YamlObj:YamlObj][:]) &&
311334
!kStr.containsChar('\n') &&
312335
kStr.size <= 1024)
313336
{
314337
out.writeChars(kStr + ":")
315338

316339
// Scalar
317-
if (v.typeof == YamlScalar# || v.content == YamlObj[,] || v.content == [YamlObj:YamlObj][:])
340+
if (v.typeof == YamlScalar# || v.val == YamlObj[,] || v.val == [YamlObj:YamlObj][:])
318341
v.writeInd(out, 1, next + 1)
319342
// Non-scalar
320343
else

src/yaml/fan/YamlParser.fan

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ internal class YamlParser
12951295
private YamlObj setLoc(YamlObj obj, FileLoc loc)
12961296
{
12971297
return Type.of(obj).make(
1298-
[obj.content,
1298+
[obj.val,
12991299
obj.tag,
13001300
loc]
13011301
)

src/yaml/fan/YamlSchema.fan

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ abstract const class YamlSchema
9797
else if (obj == null ||
9898
obj is Str) return f(obj)
9999
// Simple - needs tag
100-
else if (ser.simple) return YamlScalar(f(obj).content, "!fan/$type")
100+
else if (ser.simple) return YamlScalar(f(obj).val, "!fan/$type")
101101
// List
102102
else if (obj is List) return YamlList(YamlObj[,].addAll((obj as List).map |v| { return recEncode(v, f) }))
103103
// Map
@@ -154,6 +154,7 @@ abstract const class YamlSchema
154154
**************************************************************************
155155

156156
** YamlTagErr indicates that a YAML tag is set that does not match its node's content.
157+
@NoDoc
157158
const class YamlTagErr : Err
158159
{
159160
** Used when the given tag cannot be assigned to the given node type,
@@ -164,7 +165,7 @@ const class YamlTagErr : Err
164165
** Used when the YamlObj's tag and content do not match, e.g. combining the tag
165166
** 'tag:yaml.org,2002:int' with the content '"true"'.
166167
new makeContent(YamlObj obj)
167-
: super.make("The tag \"$obj.tag\" does not fit the content \"$obj.content\".") {}
168+
: super.make("The tag \"$obj.tag\" does not fit the content \"$obj.val\".") {}
168169

169170
new makeStr(Str msg) : super.make(msg) {}
170171
}
@@ -194,22 +195,22 @@ const class FailsafeSchema : YamlSchema
194195
switch (tag)
195196
{
196197
case "tag:yaml.org,2002:str":
197-
return node.content
198+
return node.val
198199

199200
case "tag:yaml.org,2002:seq":
200-
return (node.content as YamlObj[]).map |n| { decode(n) }
201+
return (node.val as YamlObj[]).map |n| { decode(n) }
201202

202203
case "tag:yaml.org,2002:map":
203204
res := [:]
204205
res.ordered = true
205-
map := node.content as [YamlObj:YamlObj]
206+
map := node.val as [YamlObj:YamlObj]
206207
map.keys.each |k|
207208
{
208209
k2 := decode(k)
209210
if (k2 == null)
210211
throw NullErr("Maps in Fantom cannot contain null keys.")
211212
if (res.containsKey(k2))
212-
throw YamlTagErr("The key \"$k2\", generated by the current schema from \"$k.content\", is already present in this map.")
213+
throw YamlTagErr("The key \"$k2\", generated by the current schema from \"$k.val\", is already present in this map.")
213214
res.add(decode(k).toImmutable, decode(map[k]))
214215
}
215216
return res
@@ -298,13 +299,13 @@ const class JsonSchema : FailsafeSchema
298299
return null
299300

300301
case "tag:yaml.org,2002:bool":
301-
return node.content == "true"
302+
return node.val == "true"
302303

303304
case "tag:yaml.org,2002:int":
304-
return Int(node.content)
305+
return Int(node.val)
305306

306307
case "tag:yaml.org,2002:float":
307-
return Float(node.content)
308+
return Float(node.val)
308309

309310
default:
310311
throw Err("Internal error - all tags should have been covered.")
@@ -336,12 +337,12 @@ const class JsonSchema : FailsafeSchema
336337
if (node.typeof != YamlScalar#)
337338
return super.assignTag(node)
338339

339-
if (matchNull.matches(node.content)) return "tag:yaml.org,2002:null"
340-
if (matchBool.matches(node.content)) return "tag:yaml.org,2002:bool"
341-
if (matchInt.matches(node.content)) return "tag:yaml.org,2002:int"
342-
if (matchFloat.matches(node.content)) return "tag:yaml.org,2002:float"
340+
if (matchNull.matches(node.val)) return "tag:yaml.org,2002:null"
341+
if (matchBool.matches(node.val)) return "tag:yaml.org,2002:bool"
342+
if (matchInt.matches(node.val)) return "tag:yaml.org,2002:int"
343+
if (matchFloat.matches(node.val)) return "tag:yaml.org,2002:float"
343344

344-
throw YamlTagErr("The plain content \"$node.content\" does not match any valid JSON type.")
345+
throw YamlTagErr("The plain content \"$node.val\" does not match any valid JSON type.")
345346
}
346347

347348
override Void validate(YamlObj node)
@@ -355,19 +356,19 @@ const class JsonSchema : FailsafeSchema
355356
switch (node.tag)
356357
{
357358
case "tag:yaml.org,2002:null":
358-
if (!matchNull.matches(node.content))
359+
if (!matchNull.matches(node.val))
359360
throw YamlTagErr(node)
360361

361362
case "tag:yaml.org,2002:bool":
362-
if (!matchBool.matches(node.content))
363+
if (!matchBool.matches(node.val))
363364
throw YamlTagErr(node)
364365

365366
case "tag:yaml.org,2002:int":
366-
if (!matchInt.matches(node.content))
367+
if (!matchInt.matches(node.val))
367368
throw YamlTagErr(node)
368369

369370
case "tag:yaml.org,2002:float":
370-
if (!matchFloat.matches(node.content))
371+
if (!matchFloat.matches(node.val))
371372
throw YamlTagErr(node)
372373
}
373374
}
@@ -408,7 +409,7 @@ const class CoreSchema : FailsafeSchema
408409
// Create object
409410
if (super.isRecognized(tag)) return super.decode(node)
410411

411-
cont := node.content as Str
412+
cont := node.val as Str
412413
switch (tag)
413414
{
414415
case "tag:yaml.org,2002:null":
@@ -461,10 +462,10 @@ const class CoreSchema : FailsafeSchema
461462
if (node.typeof != YamlScalar#)
462463
return super.assignTag(node)
463464

464-
if (matchNull.matches(node.content)) return "tag:yaml.org,2002:null"
465-
if (matchBool.matches(node.content)) return "tag:yaml.org,2002:bool"
466-
if (matchInt.matches(node.content)) return "tag:yaml.org,2002:int"
467-
if (matchFloat.matches(node.content)) return "tag:yaml.org,2002:float"
465+
if (matchNull.matches(node.val)) return "tag:yaml.org,2002:null"
466+
if (matchBool.matches(node.val)) return "tag:yaml.org,2002:bool"
467+
if (matchInt.matches(node.val)) return "tag:yaml.org,2002:int"
468+
if (matchFloat.matches(node.val)) return "tag:yaml.org,2002:float"
468469
return "tag:yaml.org,2002:str"
469470
}
470471

@@ -479,19 +480,19 @@ const class CoreSchema : FailsafeSchema
479480
switch (node.tag)
480481
{
481482
case "tag:yaml.org,2002:null":
482-
if (!matchNull.matches(node.content))
483+
if (!matchNull.matches(node.val))
483484
throw YamlTagErr(node)
484485

485486
case "tag:yaml.org,2002:bool":
486-
if (!matchBool.matches(node.content))
487+
if (!matchBool.matches(node.val))
487488
throw YamlTagErr(node)
488489

489490
case "tag:yaml.org,2002:int":
490-
if (!matchInt.matches(node.content))
491+
if (!matchInt.matches(node.val))
491492
throw YamlTagErr(node)
492493

493494
case "tag:yaml.org,2002:float":
494-
if (!matchFloat.matches(node.content))
495+
if (!matchFloat.matches(node.val))
495496
throw YamlTagErr(node)
496497
}
497498
}

0 commit comments

Comments
 (0)