Files
onix/plugins/implementations/coverage.html

247 lines
10 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>implementations: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">beckn-onix/plugins/implementations/plugin_impl.go (87.3%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package main
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"beckn-onix/plugins/definitions"
"github.com/santhosh-tekuri/jsonschema/v6"
)
// TekuriValidator implements the Validator interface using the santhosh-tekuri/jsonschema package.
type TekuriValidator struct {
schema *jsonschema.Schema
}
// TekuriValidatorProvider is responsible for managing and providing access to the JSON schema validators.
type TekuriValidatorProvider struct {
SchemaCache map[string]*jsonschema.Schema
}
// Validate validates the given data against the schema.
func (v *TekuriValidator) Validate(ctx context.Context, data []byte) error <span class="cov8" title="1">{
var jsonData interface{}
if err := json.Unmarshal(data, &amp;jsonData); err != nil </span><span class="cov8" title="1">{
return err
}</span>
<span class="cov8" title="1">err := v.schema.Validate(jsonData)
if err != nil </span><span class="cov8" title="1">{
// TODO: Integrate with the logging module once it is ready
fmt.Printf("Validation error: %v\n", err)
}</span>
<span class="cov8" title="1">return err</span>
}
// Initialize initializes the validator provider by compiling all the JSON schema files
// from the specified directory and storing them in a cache. It returns a map of validators
// indexed by their schema filenames.
func (vp *TekuriValidatorProvider) Initialize(schemaDir string) (map[string]definitions.Validator, error) <span class="cov8" title="1">{
// Initialize SchemaCache if it's nil
if vp.SchemaCache == nil </span><span class="cov8" title="1">{
vp.SchemaCache = make(map[string]*jsonschema.Schema)
}</span>
// Check if the directory exists and is accessible
<span class="cov8" title="1">info, err := os.Stat(schemaDir)
if err != nil </span><span class="cov8" title="1">{
if os.IsNotExist(err) </span><span class="cov8" title="1">{
return nil, fmt.Errorf("schema directory does not exist: %s", schemaDir)
}</span>
<span class="cov0" title="0">return nil, fmt.Errorf("failed to access schema directory: %v", err)</span>
}
<span class="cov8" title="1">if !info.IsDir() </span><span class="cov8" title="1">{
return nil, fmt.Errorf("provided schema path is not a directory: %s", schemaDir)
}</span>
// Initialize the validatorCache map to store the Validator instances associated with each schema.
<span class="cov8" title="1">validatorCache := make(map[string]definitions.Validator)
compiler := jsonschema.NewCompiler()
// Helper function to process directories recursively
var processDir func(dir string) error
processDir = func(dir string) error </span><span class="cov8" title="1">{
entries, err := os.ReadDir(dir)
if err != nil </span><span class="cov0" title="0">{
return fmt.Errorf("failed to read directory: %v", err)
}</span>
<span class="cov8" title="1">for _, entry := range entries </span><span class="cov8" title="1">{
path := filepath.Join(dir, entry.Name())
if entry.IsDir() </span><span class="cov8" title="1">{
// Recursively process subdirectories
if err := processDir(path); err != nil </span><span class="cov8" title="1">{
return err
}</span>
} else<span class="cov8" title="1"> if filepath.Ext(entry.Name()) == ".json" </span><span class="cov8" title="1">{
// Process JSON files
compiledSchema, err := compiler.Compile(path)
if err != nil </span><span class="cov8" title="1">{
return fmt.Errorf("failed to compile JSON schema from file %s: %v", entry.Name(), err)
}</span>
<span class="cov8" title="1">if compiledSchema == nil </span><span class="cov0" title="0">{
return fmt.Errorf("compiled schema is nil for file %s", entry.Name())
}</span>
// Use relative path from schemaDir to avoid absolute paths and make schema keys domain/version specific.
<span class="cov8" title="1">relativePath, err := filepath.Rel(schemaDir, path)
if err != nil </span><span class="cov0" title="0">{
return fmt.Errorf("failed to get relative path for file %s: %v", entry.Name(), err)
}</span>
// Ensure relativePath is not empty
<span class="cov8" title="1">if relativePath == "" </span><span class="cov0" title="0">{
return fmt.Errorf("relative path is empty for file: %s", path)
}</span>
// Split the relative path to get domain, version, and schema.
<span class="cov8" title="1">parts := strings.Split(relativePath, string(os.PathSeparator))
// Ensure that the file path has at least 3 parts: domain, version, and schema file.
if len(parts) &lt; 3 </span><span class="cov0" title="0">{
return fmt.Errorf("invalid schema file structure, expected domain/version/schema.json but got: %s", relativePath)
}</span>
// Extract domain, version, and schema filename from the parts.
// Validate that the extracted parts are non-empty
<span class="cov8" title="1">domain := strings.TrimSpace(parts[0])
version := strings.TrimSpace(parts[1])
schemaFileName := strings.TrimSpace(parts[2])
if domain == "" || version == "" || schemaFileName == "" </span><span class="cov0" title="0">{
return fmt.Errorf("invalid schema file structure, one or more components are empty. Relative path: %s", relativePath)
}</span>
// Construct a unique key combining domain, version, and schema name (e.g., ondc_trv10/v2.0.0/schema.json).
<span class="cov8" title="1">uniqueKey := fmt.Sprintf("%s/%s/%s", domain, version, schemaFileName)
// Store the compiled schema in the SchemaCache using the unique key.
vp.SchemaCache[uniqueKey] = compiledSchema
// Store the corresponding validator in the validatorCache using the same unique key.
validatorCache[uniqueKey] = &amp;TekuriValidator{schema: compiledSchema}</span>
}
}
<span class="cov8" title="1">return nil</span>
}
// Start processing from the root schema directory
<span class="cov8" title="1">if err := processDir(schemaDir); err != nil </span><span class="cov8" title="1">{
return nil, fmt.Errorf("failed to read schema directory: %v", err)
}</span>
<span class="cov8" title="1">return validatorCache, nil</span>
}
var _ definitions.ValidatorProvider = (*TekuriValidatorProvider)(nil)
var providerInstance = &amp;TekuriValidatorProvider{}
// GetProvider returns the ValidatorProvider instance.
func GetProvider() definitions.ValidatorProvider <span class="cov8" title="1">{
return providerInstance
}</span>
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>