-
Notifications
You must be signed in to change notification settings - Fork 14
Add TOML parser and schema validator #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
heshanpadmasiri
merged 8 commits into
ballerina-platform:main
from
snelusha:toml-parser
Nov 17, 2025
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b349f61
Add TOML parser and schema validator
snelusha a92bac7
Add error locations to diagnostics
snelusha 1b9d1ae
Update license header format
snelusha 9ff6053
Extract `readFile` helper function
snelusha 2c5747c
Refactor schema loading from path and reader
snelusha 8517726
Use `fs.FS` interface for file system access
snelusha a1adbb9
Refactor diagnostic severity to use the enum
snelusha 25e2234
Remove unused `RootNode()` method
snelusha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| module ballerina-lang-go | ||
|
|
||
| go 1.24.4 | ||
|
|
||
| require github.com/BurntSushi/toml v1.5.0 | ||
|
|
||
| require ( | ||
| github.com/santhosh-tekuri/jsonschema/v6 v6.0.2 // indirect | ||
| golang.org/x/text v0.14.0 // indirect | ||
| ) |
This file contains hidden or 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,6 @@ | ||
| github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= | ||
| github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= | ||
| github.com/santhosh-tekuri/jsonschema/v6 v6.0.2 h1:KRzFb2m7YtdldCEkzs6KqmJw4nqEVZGK7IN2kJkjTuQ= | ||
| github.com/santhosh-tekuri/jsonschema/v6 v6.0.2/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU= | ||
| golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= | ||
| golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= |
This file contains hidden or 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,94 @@ | ||
| // Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package tomlparser | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "io/fs" | ||
| "os" | ||
|
|
||
| "github.com/santhosh-tekuri/jsonschema/v6" | ||
| ) | ||
|
|
||
| type Schema interface { | ||
| Validate(data any) error | ||
| FromPath(fsys fs.FS, path string) (Schema, error) | ||
| FromString(content string) (Schema, error) | ||
| } | ||
|
|
||
| type schemaImpl struct { | ||
| compiled *jsonschema.Schema | ||
| } | ||
|
|
||
| func NewSchemaFromPath(fsys fs.FS, path string) (Schema, error) { | ||
| content, err := readFile(fsys, path) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read schema file %s: %w", path, err) | ||
| } | ||
| return NewSchemaFromString(content) | ||
| } | ||
|
|
||
| func NewSchemaFromString(content string) (Schema, error) { | ||
| compiler := jsonschema.NewCompiler() | ||
|
|
||
| var schemaDoc any | ||
| if err := json.Unmarshal([]byte(content), &schemaDoc); err != nil { | ||
| return nil, fmt.Errorf("failed to parse schema JSON: %w", err) | ||
| } | ||
|
|
||
| if err := compiler.AddResource("schema.json", schemaDoc); err != nil { | ||
| return nil, fmt.Errorf("failed to add schema resource: %w", err) | ||
| } | ||
|
|
||
| schema, err := compiler.Compile("schema.json") | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to compile schema: %w", err) | ||
| } | ||
|
|
||
| return &schemaImpl{ | ||
| compiled: schema, | ||
| }, nil | ||
| } | ||
|
|
||
| func NewSchemaFromReader(reader io.Reader) (Schema, error) { | ||
| content, err := readFromReader(reader) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read schema: %w", err) | ||
| } | ||
| return NewSchemaFromString(content) | ||
| } | ||
|
|
||
| func NewSchemaFromFile(file *os.File) (Schema, error) { | ||
snelusha marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return NewSchemaFromReader(file) | ||
| } | ||
|
|
||
| func (s *schemaImpl) Validate(data any) error { | ||
| if err := s.compiled.Validate(data); err != nil { | ||
| return fmt.Errorf("schema validation failed: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *schemaImpl) FromPath(fsys fs.FS, path string) (Schema, error) { | ||
| return NewSchemaFromPath(fsys, path) | ||
| } | ||
|
|
||
| func (s *schemaImpl) FromString(content string) (Schema, error) { | ||
| return NewSchemaFromString(content) | ||
| } | ||
This file contains hidden or 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,9 @@ | ||
| [package] | ||
| org = "foo" | ||
| name = "winery" | ||
| version = "0.1.0" | ||
| license = ["MIT", "Apache-2.0"] | ||
| authors = ["jo@wso2.com", "pramodya@wso2.com"] | ||
| repository = "https://github.com/ballerinalang/ballerina" | ||
| keywords = ["ballerina", "security", "crypto"] | ||
| exported = ["winery", "service"] |
This file contains hidden or 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,10 @@ | ||
| [package] | ||
| org = "foo" | ||
| name = "winery" | ||
| version = "0.1.0" | ||
| license = ["MIT", "Apache-2.0"] | ||
| authors = ["jo@wso2.com", "pramodya@wso2.com"] | ||
| repository = "https://github.com/ballerinalang/ballerina" | ||
| keywords = ["ballerina", "security", "crypto"] | ||
| exported = ["winery", "service"] | ||
| unexpected = "this field is not in schema" |
This file contains hidden or 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,2 @@ | ||
| name = "test" | ||
| description = "missing version field" |
This file contains hidden or 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,19 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "title": "Config Schema", | ||
| "description": "Schema with required fields", | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "required": ["name", "version"], | ||
| "properties": { | ||
| "name": { | ||
| "type": "string" | ||
| }, | ||
| "version": { | ||
| "type": "string" | ||
| }, | ||
| "description": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,51 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "title": "Ballerina Manifest Spec", | ||
| "description": "Schema for Ballerina Manifest", | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "package": { | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "name": { | ||
| "type": "string" | ||
| }, | ||
| "org": { | ||
| "type": "string" | ||
| }, | ||
| "version": { | ||
| "type": "string" | ||
| }, | ||
| "license": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "authors": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "repository": { | ||
| "type": "string" | ||
| }, | ||
| "keywords": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "exported": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,28 @@ | ||
| title = "Server Configuration" | ||
|
|
||
| [owner] | ||
| name = "WSO2" | ||
|
|
||
| [database] | ||
| server = "192.168.1.1" | ||
| ports = [ 8001, 8001, 8002 ] | ||
| connection_max = 5000 | ||
| enabled = true | ||
|
|
||
| [servers] | ||
| [servers.alpha] | ||
| ip = "10.0.0.1" | ||
| dc = "eqdc10" | ||
|
|
||
| [servers.beta] | ||
| ip = "10.0.0.2" | ||
| dc = "eqdc10" | ||
|
|
||
| [[routes]] | ||
| name = "health" | ||
| path = "/health" | ||
|
|
||
| [[routes]] | ||
| name = "metrics" | ||
| path = "/metrics" | ||
| method = "GET" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.