Code Monkey home page Code Monkey logo

cray's Introduction

CRay

test codecov

Access configs in a structured manner with validation and automatically generate documented configs.

CRay creates a schema for your data by tracking how you access it. You don't have to keep checking each value in the middle of your code to see if it satisfies the condition you want. Validate documents at once with an automatically generated schema, and see at a glance what values went wrong.

Support

Loaders

Reporters

  • YAML
  • JSON Schema

CMake Integration

include(FetchContent)
FetchContent_Declare(
  CRay
  GIT_REPOSITORY https://github.com/lesomnus/cray.git
  GIT_TAG        main
)
FetchContent_MakeAvailable(CRay)

...

add_library(foo ...)
...
target_link_libraries(
	foo PRIVATE
		CRay::CRay
		CRay::yaml  # YAML loader support
)

Note that CRay::yaml is enabled only if the yaml-cpp package is available.

Example

You may want to read what kind of describers are available.

#include <fstream>
#include <iostream>
#include <optional>
#include <sstream>
#include <string>

#include <cray.hpp>

struct Step {
	std::string name;
	std::string run;
};

struct Job {
	std::vector<std::string> runs_on;
	std::vector<Step>        steps;
};

int main(int argc, char*[] argv) {
	using namespace cray;
	
	Node doc(load::fromYaml("workflow.yaml"));

	auto const name = doc["name"].as<std::string>(Annotation{
	    .title       = "Name",
	    .description = "The name of your workflow.",
	});

	auto const run_name = doc["run-name"].as<std::optional<std::string>>(Annotation{
	    .title       = "Run Name",
	    .description = "The name for workflow runs generated from the workflow.",
	});

	auto events = prop<Type::Str>().oneOf({
	    "push",
	    "pull_request",
	    "workflow_dispatch",
	});

	auto const on = doc["on"].is<Type::List>().of(events).get();

	auto step =
	    prop<Type::Map>().to<Step>()
	    | field("name", &Step::name)
	    | field("run", &Step::run);

	auto job =
	    prop<Type::Map>().to<Job>()
	    | field("runs-on", &Job::runs_on)
	    | field("steps", &Job::steps, step);

	auto const jobs = doc["jobs"].is<Type::Map>().of(job).get();

	std::ofstream out("workflow.yaml");
	report::asYaml(out, doc);
	
	std::ofstream out("workflow.schema.json");
	report::asJsonSchema(out, doc);

	if(!doc.ok()){
		// There is a field that does not satisfy the condition or has the wrong type.
		return -1;
	}

	// name == "Test"
	// jobs["test"].steps[0].run == "build && test"
	// ...
}

Consider original workflow.yaml:

name: Test
on: [push, pull_request]
jobs:
  test:
    runs-on: [ubuntu-20.04]
    steps:
      - name: Test
        run: build && test

Will be:

# Name
# | The name of your workflow.
name: Test

# Run Name
# | The name for workflow runs generated from the workflow.
run-name:   # <String>

on: 
  # • push | pull_request | workflow_dispatch
  [push, pull_request]
jobs: 
  test: 
    runs-on: [ubuntu-20.04]
    steps: 
      - 
        name: Test
        run: build && test

And you can have JSON schema workflow.schema.json:

This is a summarized result. Full results can be found at tests/src/example-report.cpp.

{
	"type": "object",
	"required": [
		"name",
		"on",
		"jobs"
	],
	"properties": {
		"name": {
			"type": "string",
			"title": "Name",
			"description": "The name of your workflow."
		},
		"run-name": {
			"type": "string",
			"title": "Run Name",
			"description": "The name for workflow runs generated from the workflow."
		},
		"on": {
			"type": "array",
			"items": {
				"type": "string",
				"enum": [
					"push",
					"pull_request",
					"workflow_dispatch"
				]
			}
		},
		"jobs": {
			...
		}
	}
}

cray's People

Contributors

lesomnus avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.