Giter Site home page Giter Site logo

caps's Introduction

Go code generator for Capnproto definitions. It integrates MsgPack and Capn'proto code generators. Code generation options available as annotations. Also this tool adds support for possible data validation.

HOW TO

You need Go 1.5 version. Set environment variable: GO15VENDOREXPERIMENT=1.

  1. Install capnp tool. See Instructions

  2. Install caps

    go get github.com/tpukep/caps/...
  3. Write Capn'proto schema model.capnp:

    @0xad8d3cbc6db52a1d;
    
    using Go = import "/go.capnp";
    $Go.package("model");
    
    struct Book {
       title      @0:   Text;
       pageCount  @1:   Int32;
       authors    @2:   List(Text);
       content    @3:   Text;
    }
  4. Generate Go plain code.

    caps -source model.capnp

Annotations

Codecs

using Codec = import "/caps/codec.capnp";

# By default only plain Go code will be generated
# These annotations are enable extra features

$Codec.msgp;  # Enables msgp code generation
$Codec.json;  # Enables go json tags generation in plain Go code
$Codec.capnp; # Enables Capn'proto code generation

struct Person {
   name  @0 :Text;
   email @1 :Text;
   age   @2 :UInt8;
   phone @3 :Text;
}

Fields

using Field = import "/caps/field.capnp";

struct Person {
   firstName  @0 :Text $Field.required("name"); # Rename field to use more
                                                # compact name in serialization code
   email @1 :Text;
   age   @2 :UInt8 $Field.optional("pc");      # Mark as optional
   phone @3 :Text $Field.ignored;              # This field will be ignored
}

Checks

You can use Go-playground Validator expressions to generate tags in plain Go code. Note that $Field tags also generate corresponding validate tags.

Example of various checks

using Check = import "/caps/check.capnp";

struct Person $Go.doc("Some Person") {
   name  @0 :Text  $Check.value("max=256,min=2");
   email @1 :Text  $Check.value("email");
   age   @2 :UInt8 $Check.value("max=40");
   phone @3 :Text;
}

Examples of use located at demo/annotations.capnp.

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.