local SpecViewCheck = {} local function rawpairs( t ) return next, t, nil end function SpecViewCheck.Verify( formFile, num_level ) local ret = 0 local form = "" local message = "" local inBody = false local utils = require "ExtUtils" function viewBody( line ) if string.match( line, "^\n") then return true, "" end local tline = utils.trim( line ) local _, _, lhs, rhs = string.find( tline, "(.*) (\"?//.*)" ) -- Restrictions do not apply on excluded mapping (beginning with -) if string.match( tline, "^\"%-" ) or string.match( tline, "^%-" ) then return true, "" end if lhs == nil then -- malformed view, let the server reject the view return true, "" end -- move past // local idx = string.find( lhs, "//") if not idx then error( "missing // lhs=" .. lhs .. ">" ) return false, "Missing delimiter" end -- count dirlevel local str = string.sub( lhs, idx + 2 ) local _, dirlevel = string.gsub( str, "/", "") if dirlevel == nil then dirlevel = 0 end if dirlevel >= num_level then return true, "" else local message = "For line " .. line .. ", not specific enough, only " .. dirlevel .. " directory level when " .. num_level .. " needed" return false, message end end local f = io.open( formFile, "r" ) if f == nil then error( "Upexpected error, form file does not exist:" .. formFile .. "\n" ) local message = "Upexpected internal error, form file does not exist" return false, message end local contents = f:read( "*all" ) local p4 = P4.P4:new() local parsed = p4:parse_spec( "client", contents ) local view = parsed[ "View" ] for k, v in ipairs(view) do line = v if string.match( line, "^#" ) then goto continue end ret, message = viewBody( line ) if not ret then return ret, message end ::continue:: end return true, "no message" end return SpecViewCheck