Otter
Packs
Otter
Upstream Contract & Behavioral Tests
file-helpers Pack
Safe file I/O and JSON storage utilities: readOrFallback, writeSafe, appendSafe, deleteFileSafe, fileExistsSafe, toJsonSafe, fromJsonSafe, readJsonObject, readJsonObjectSafe, and writeJsonObject.
Schema v2 Operation Contract (10 operations)
Scoping Prefix:_fh_
Schema v2 Pack exposing 10 structurally AST-verified Otter 1.0.1 operations with isolated prefix '_fh_'.
| Operation Signature | Returns | Side Effects | Error Mode |
|---|---|---|---|
fileExistsSafe(filePath: text) |
boolean |
filesystem:read | returns-false |
readOrFallback(filePath: text, fallbackValue: any) |
any |
filesystem:read | returns-fallback |
writeSafe(filePath: text, content: any) |
boolean |
filesystem:write | returns-false |
appendSafe(filePath: text, content: any) |
boolean |
filesystem:write | returns-false |
deleteFileSafe(filePath: text) |
boolean |
filesystem:write | returns-false |
toJsonSafe(obj: any, fallbackValue: any) |
text |
none | returns-fallback |
fromJsonSafe(rawJson: text, fallbackValue: any) |
thing |
none | returns-fallback |
readJsonObject(filePath: text) |
thing |
filesystem:read | returns-gone |
readJsonObjectSafe(filePath: text, fallbackValue: any) |
thing |
filesystem:read | returns-fallback |
writeJsonObject(filePath: text, obj: any) |
boolean |
filesystem:write | returns-false |
Source Code (4 files)
# file-helpers - Otter Reusable File & Storage Library
# Provides readOrFallback, writeSafe, appendSafe, deleteFileSafe, fileExistsSafe,
# toJsonSafe, fromJsonSafe, readJsonObject, readJsonObjectSafe, and writeJsonObject
# Internal variables use '_fh_' prefix to prevent collisions with caller/outer variables.
to fileExistsSafe filePath
if file filePath exists
return true
.
return false
.
to readOrFallback filePath fallbackValue
if file filePath exists
try
read filePath into _fh_content
return _fh_content
otherwise
return fallbackValue
.
.
return fallbackValue
.
to writeSafe filePath content
try
write content to filePath
return true
otherwise
return false
.
.
to appendSafe filePath content
try
append content to filePath
return true
otherwise
return false
.
.
to deleteFileSafe filePath
try
if file filePath exists
delete file filePath
return true
.
otherwise
return false
.
return false
.
to toJsonSafe obj fallbackValue
try
convert obj to json into _fh_raw
return _fh_raw
otherwise
return fallbackValue
.
.
to fromJsonSafe rawJson fallbackValue
try
_fh_target is a thing
convert rawJson from json into _fh_target
return _fh_target
otherwise
return fallbackValue
.
.
to readJsonObject filePath
try
if file filePath exists
read filePath into _fh_rawJson
_fh_target is a thing
convert _fh_rawJson from json into _fh_target
return _fh_target
.
otherwise
return gone
.
return gone
.
to readJsonObjectSafe filePath fallbackValue
try
if file filePath exists
read filePath into _fh_rawJson
_fh_target is a thing
convert _fh_rawJson from json into _fh_target
return _fh_target
.
otherwise
return fallbackValue
.
return fallbackValue
.
to writeJsonObject filePath obj
try
convert obj to json into _fh_rawJson
write _fh_rawJson to filePath
return true
otherwise
return false
.
.
{
"schemaVersion": 2,
"name": "file-helpers",
"publisher": "otter-official",
"version": "1.0.1",
"kind": "pack",
"description": "Safe file I/O and JSON storage utilities: readOrFallback, writeSafe, appendSafe, deleteFileSafe, fileExistsSafe, toJsonSafe, fromJsonSafe, readJsonObject, readJsonObjectSafe, and writeJsonObject.",
"entry": "src/main.ot",
"license": "MIT",
"requiresOtter": ">=1.0.1",
"platforms": [
"console",
"desktop"
],
"variablePrefix": "_fh_",
"provides": {
"capabilities": [
"persistence.filesystem",
"persistence.json"
],
"operations": [
{
"name": "fileExistsSafe",
"description": "Checks whether a file exists safely on the filesystem.",
"parameters": [
{ "name": "filePath", "type": "text" }
],
"returns": { "type": "boolean" },
"sideEffects": ["filesystem:read"],
"errorMode": "returns-false"
},
{
"name": "readOrFallback",
"description": "Safely reads file content as text, or returns fallbackValue if file does not exist or read fails.",
"parameters": [
{ "name": "filePath", "type": "text" },
{ "name": "fallbackValue", "type": "any" }
],
"returns": { "type": "any" },
"sideEffects": ["filesystem:read"],
"errorMode": "returns-fallback"
},
{
"name": "writeSafe",
"description": "Safely writes text content to filePath, returning true on success or false on failure.",
"parameters": [
{ "name": "filePath", "type": "text" },
{ "name": "content", "type": "any" }
],
"returns": { "type": "boolean" },
"sideEffects": ["filesystem:write"],
"errorMode": "returns-false"
},
{
"name": "appendSafe",
"description": "Safely appends text content to filePath, returning true on success or false on failure.",
"parameters": [
{ "name": "filePath", "type": "text" },
{ "name": "content", "type": "any" }
],
"returns": { "type": "boolean" },
"sideEffects": ["filesystem:write"],
"errorMode": "returns-false"
},
{
"name": "deleteFileSafe",
"description": "Safely deletes a file if it exists, returning true on success or false on failure.",
"parameters": [
{ "name": "filePath", "type": "text" }
],
"returns": { "type": "boolean" },
"sideEffects": ["filesystem:write"],
"errorMode": "returns-false"
},
{
"name": "toJsonSafe",
"description": "Converts an object or thing to JSON text, returning fallbackValue on serialization error.",
"parameters": [
{ "name": "obj", "type": "any" },
{ "name": "fallbackValue", "type": "any" }
],
"returns": { "type": "text" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "fromJsonSafe",
"description": "Parses a JSON string into a thing object, returning fallbackValue on deserialization error.",
"parameters": [
{ "name": "rawJson", "type": "text" },
{ "name": "fallbackValue", "type": "any" }
],
"returns": { "type": "thing" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "readJsonObject",
"description": "Reads a JSON file from disk into a thing object, returning gone if the file does not exist or fails to parse.",
"parameters": [
{ "name": "filePath", "type": "text" }
],
"returns": { "type": "thing", "nullable": true },
"sideEffects": ["filesystem:read"],
"errorMode": "returns-gone"
},
{
"name": "readJsonObjectSafe",
"description": "Reads a JSON file from disk into a thing object, returning fallbackValue if the file does not exist or fails to parse.",
"parameters": [
{ "name": "filePath", "type": "text" },
{ "name": "fallbackValue", "type": "any" }
],
"returns": { "type": "thing" },
"sideEffects": ["filesystem:read"],
"errorMode": "returns-fallback"
},
{
"name": "writeJsonObject",
"description": "Serializes an object or thing to JSON and writes it to filePath on disk, returning true on success or false on failure.",
"parameters": [
{ "name": "filePath", "type": "text" },
{ "name": "obj", "type": "any" }
],
"returns": { "type": "boolean" },
"sideEffects": ["filesystem:write"],
"errorMode": "returns-false"
}
]
},
"requires": {
"capabilities": [],
"packages": {}
},
"dependencies": {},
"tags": [
"files",
"io",
"json",
"storage",
"read",
"write",
"persistence"
],
"examples": [
"examples/demo.ot"
]
}
# Tests for file-helpers (Strict Behavioral Assertions)
use "../src/main.ot"
say "Running file-helpers behavioral tests..."
get system folder "temp" into tempFolder
testTxtFile is tempFolder + "test_helpers_temp.txt"
testJsonFile is tempFolder + "test_helpers_temp.json"
# Clean up before testing
deleteFileSafe testTxtFile
deleteFileSafe testJsonFile
# 1. Missing file returns fallback
m1 is readOrFallback "non_existent_file_12345.txt" "DEFAULT"
if m1 is not "DEFAULT"
fail with "readOrFallback failed: expected DEFAULT for non-existent file"
.
# 2. fileExistsSafe
if fileExistsSafe "non_existent_file_12345.txt" is not false
fail with "fileExistsSafe failed: expected false for missing file"
.
# 3. Write and read text
wOk is writeSafe testTxtFile "Initial text content\n"
if wOk is not true
fail with "writeSafe failed: write returned false"
.
if fileExistsSafe testTxtFile is not true
fail with "fileExistsSafe failed: expected file to exist after write"
.
readBack is readOrFallback testTxtFile "FAIL"
if readBack is not "Initial text content\n"
fail with "readOrFallback mismatch after write"
.
# 4. Append text
aOk is appendSafe testTxtFile "Appended second line\n"
if aOk is not true
fail with "appendSafe failed: append returned false"
.
appendedContent is readOrFallback testTxtFile "FAIL"
if not appendedContent contains "Appended second line"
fail with "appendSafe failed: content did not contain appended line"
.
# 5. JSON serialization and deserialization
obj has
title is "Config Settings"
port is 8080
debug is true
.
jsonStr is toJsonSafe obj "{}"
if not jsonStr contains "8080"
fail with "toJsonSafe failed: output missing port 8080"
.
# 6. Corrupt JSON deserialization safeguard
corruptJson is "{ this is invalid json !!!"
fallbackVal has
fallbackKey is "active"
.
recoveredObj is fromJsonSafe corruptJson fallbackVal
if fallbackKey of recoveredObj is not "active"
fail with "fromJsonSafe failed: corrupt JSON did not return fallback value"
.
# 7. Write and read JSON file
wJsonOk is writeJsonObject testJsonFile obj
if wJsonOk is not true
fail with "writeJsonObject failed: returned false"
.
loadedObj is readJsonObject testJsonFile
if title of loadedObj is not "Config Settings"
fail with "readJsonObject failed: title mismatch"
.
if port of loadedObj is not 8080
fail with "readJsonObject failed: port mismatch"
.
# 8. Missing JSON file safe read
missingJsonObj is readJsonObjectSafe "does_not_exist.json" fallbackVal
if fallbackKey of missingJsonObj is not "active"
fail with "readJsonObjectSafe failed: missing file did not return fallback"
.
# 9. Delete files and confirm removal
deleteFileSafe testTxtFile
deleteFileSafe testJsonFile
if fileExistsSafe testTxtFile is not false
fail with "deleteFileSafe failed: text file still exists"
.
if fileExistsSafe testJsonFile is not false
fail with "deleteFileSafe failed: json file still exists"
.
say "All file-helpers behavioral tests passed successfully!"
# file-helpers Demo
use "../src/main.ot"
say "=== file-helpers Demo ==="
demoFile is "demo_store.json"
config has
environment is "production"
logLevel is "info"
maxRetries is 5
.
say "Saving configuration object to" demoFile "..."
writeJsonObject demoFile config
say "File exists:" fileExistsSafe demoFile
say "Reading configuration back..."
loaded is readJsonObject demoFile
say "Loaded environment:" environment of loaded
say "Loaded log level:" logLevel of loaded
say "Loaded max retries:" maxRetries of loaded
say "Testing corrupt JSON fallback..."
corruptFallback has
environment is "fallback-env"
.
safeObj is fromJsonSafe "{ malformed" corruptFallback
say "Recovered environment from corrupt JSON:" environment of safeObj
say "Cleaning up demo file..."
deleteFileSafe demoFile
say "File exists after delete:" fileExistsSafe demoFile
Installation & Usage
otter pack install otter-official/file-helpers
otter pack verify --contracts
use ".otter/packages/otter-official/file-helpers/1.0.1/src/main.ot" # Inspect exported operation contracts: otter pack contract otter-official/file-helpers