-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Validates property types passed in --config. - Types are stored in properties.json.
- Loading branch information
Showing
7 changed files
with
153 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"os" | ||
"reflect" | ||
) | ||
|
||
type Property struct { | ||
Property string `json:"property"` | ||
Type string `json:"type"` | ||
} | ||
|
||
func validateConfig(configMap map[string]interface{}) { | ||
properties := loadProperties() | ||
|
||
// Create a map for quick lookup of property types | ||
propertyTypes := make(map[string]string) | ||
for _, prop := range properties { | ||
propertyTypes[prop.Property] = prop.Type | ||
} | ||
|
||
for key, value := range configMap { | ||
expectedType, exists := propertyTypes[key] | ||
if !exists { | ||
fmt.Printf("Warning: Unknown property %s in config\n", key) | ||
continue | ||
} | ||
|
||
if !validateType(value, expectedType) { | ||
fmt.Printf("Invalid type for property %s. Expected %s, got %T\n", key, expectedType, value) | ||
os.Exit(1) | ||
} | ||
} | ||
} | ||
|
||
func loadProperties() []Property { | ||
propertiesPath := getPath("properties.json") | ||
var properties []Property | ||
parseJson(propertiesPath, &properties) | ||
return properties | ||
} | ||
|
||
func validateType(value interface{}, expectedType string) bool { | ||
switch expectedType { | ||
case "str": | ||
_, ok := value.(string) | ||
return ok | ||
case "int": | ||
v, ok := value.(float64) | ||
return ok && v == math.Trunc(v) | ||
case "uint": | ||
v, ok := value.(float64) | ||
return ok && v == math.Trunc(v) && v >= 0 | ||
case "double": | ||
_, ok := value.(float64) | ||
return ok | ||
case "bool": | ||
_, ok := value.(bool) | ||
return ok | ||
case "array": | ||
return reflect.TypeOf(value).Kind() == reflect.Slice | ||
default: | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
[ | ||
{ "property": "navigator.userAgent", "type": "str" }, | ||
{ "property": "navigator.doNotTrack", "type": "str" }, | ||
{ "property": "navigator.appCodeName", "type": "str" }, | ||
{ "property": "navigator.appName", "type": "str" }, | ||
{ "property": "navigator.appVersion", "type": "str" }, | ||
{ "property": "navigator.oscpu", "type": "str" }, | ||
{ "property": "navigator.language", "type": "str" }, | ||
{ "property": "navigator.languages", "type": "array" }, | ||
{ "property": "navigator.platform", "type": "str" }, | ||
{ "property": "navigator.hardwareConcurrency", "type": "uint" }, | ||
{ "property": "navigator.product", "type": "str" }, | ||
{ "property": "navigator.productSub", "type": "str" }, | ||
{ "property": "navigator.maxTouchPoints", "type": "uint" }, | ||
{ "property": "navigator.cookieEnabled", "type": "bool" }, | ||
{ "property": "navigator.globalPrivacyControl", "type": "bool" }, | ||
{ "property": "navigator.buildID", "type": "str" }, | ||
{ "property": "navigator.onLine", "type": "bool" }, | ||
{ "property": "screen.availHeight", "type": "uint" }, | ||
{ "property": "screen.availWidth", "type": "uint" }, | ||
{ "property": "screen.availTop", "type": "uint" }, | ||
{ "property": "screen.availLeft", "type": "uint" }, | ||
{ "property": "screen.height", "type": "uint" }, | ||
{ "property": "screen.width", "type": "uint" }, | ||
{ "property": "screen.colorDepth", "type": "uint" }, | ||
{ "property": "screen.pixelDepth", "type": "uint" }, | ||
{ "property": "screen.pageXOffset", "type": "double" }, | ||
{ "property": "screen.pageYOffset", "type": "double" }, | ||
{ "property": "window.scrollMinX", "type": "int" }, | ||
{ "property": "window.scrollMinY", "type": "int" }, | ||
{ "property": "window.scrollMaxX", "type": "int" }, | ||
{ "property": "window.scrollMaxY", "type": "int" }, | ||
{ "property": "window.outerHeight", "type": "uint" }, | ||
{ "property": "window.outerWidth", "type": "uint" }, | ||
{ "property": "window.innerHeight", "type": "uint" }, | ||
{ "property": "window.innerWidth", "type": "uint" }, | ||
{ "property": "window.screenX", "type": "int" }, | ||
{ "property": "window.screenY", "type": "int" }, | ||
{ "property": "window.history.length", "type": "uint" }, | ||
{ "property": "window.devicePixelRatio", "type": "double" }, | ||
{ "property": "document.body.clientWidth", "type": "uint" }, | ||
{ "property": "document.body.clientHeight", "type": "uint" }, | ||
{ "property": "document.body.clientTop", "type": "uint" }, | ||
{ "property": "document.body.clientLeft", "type": "uint" }, | ||
{ "property": "headers.User-Agent", "type": "str" }, | ||
{ "property": "headers.Accept-Language", "type": "str" }, | ||
{ "property": "headers.Accept-Encoding", "type": "str" }, | ||
{ "property": "webrtc:ipv4", "type": "str" }, | ||
{ "property": "webrtc:ipv6", "type": "str" }, | ||
{ "property": "pdfViewerEnabled", "type": "bool" }, | ||
{ "property": "webGl:renderer", "type": "str" }, | ||
{ "property": "webGl:vendor", "type": "str" }, | ||
{ "property": "battery:charging", "type": "bool" }, | ||
{ "property": "battery:chargingTime", "type": "double" }, | ||
{ "property": "battery:dischargingTime", "type": "double" }, | ||
{ "property": "battery:level", "type": "double" }, | ||
{ "property": "fonts", "type": "array" } | ||
] |