forked from one-data-model/playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointercheck.rb
85 lines (80 loc) · 1.61 KB
/
pointercheck.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env ruby
require 'json'
unless ARGV.size > 0
ARGV.concat Dir['./**/*.sdf.json']
end
jps = %w[
sdfRef
sdfRequired
]
jps_old = %w[
sdfInputData
sdfRequiredInputData
sdfOutputData
]
# Walk down a path of map keys $[label1][label2] and return failing index
def walk(position, labels)
labels.each_with_index do |l, i|
if position.has_key?(l)
position = position[l]
else
return i
end
end
false
end
# Search for a map key and return value, $..["label"]
# mutating "input"!
def descend(input, position, label)
# p [input, position, label]
case position
when Hash
if position.has_key?(label)
input << position[label]
end
position.each { |k, v|
descend(input, v, label)
}
when Array
position.each { |v|
descend(input, v, label)
}
end
input
end
ARGV.each do |fn|
begin
spec = JSON.load(File.read(fn))
rescue Exception => e
puts [fn, e].inspect
next
end
copr = spec["info"]["copyright"].sub(/Copyright ?(\(c\))? ?(20..(-20..)?)?,? ?/, "")
.sub(/ ?All rights reserved./, "")
bad = []
pointers = jps.map {|jp| descend([], spec, jp)}.flatten
pointers.each { |pt|
pl = pt.split("/")
f = nil
unless pl[0] == "#"
f = -1
else
f = walk(spec, pl[1..-1])
end
if f
f += 1
pl[f] = ">>#{pl[f]}<<"
bad << pl.join("/")
end
}
jps_old.map {|jp| descend([], spec, jp)}.flatten.each do |pt|
if String === pt
puts "#{fn} (#{copr}): pre-1.1 reference #{pt}"
end
end
if bad != []
puts "#{fn} (#{copr}):"
bad.each {|b| puts "- #{b}"}
puts
end
end