genpb

package
v0.4.4-alpha.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"
	"log"

	"kusionstack.io/kclvm-go/pkg/tools/genpb"
)

var _ = genpb.GenProto

func main() {
	const code = `
import units

#kclvm/genpb: option go_package = kcl_gen/_/hello
#kclvm/genpb: option pb_package = kcl_gen._.hello

type NumberMultiplier = units.NumberMultiplier

schema Person:
	"""Person Example"""
	name: str = "kcl"
	age: int = 2
	friends?: [str] = None
	movies?: {str: Movie} = None

schema Movie:
	desc: str = ""
	size: NumberMultiplier = 2M
	kind?: "Superhero" | "War" | "Unknown"
	unknown1?: int | str = None
	unknown2?: any = None
`

	pbCode, err := genpb.GenProto("hello.k", code, nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(pbCode)

}
Output:

syntax = "proto3";

package kcl_gen._.hello;

option go_package = "kcl_gen/_/hello";

import "google/protobuf/any.proto";

// Person Example
message Person {
    string name = 1;               // kcl-type: str
    int64 age = 2;                 // kcl-type: int
    repeated string friends = 3;   // kcl-type: [str]
    map<string, Movie> movies = 4; // kcl-type: {str:Movie}
}

message Movie {
    string desc = 1;                  // kcl-type: str
    int64 size = 2;                   // kcl-type: units.NumberMultiplier
    string kind = 3;                  // kcl-type: "Superhero"|"War"|"Unknown"
    google.protobuf.Any unknown1 = 4; // kcl-type: int|str
    google.protobuf.Any unknown2 = 5; // kcl-type: any
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenProto

func GenProto(filename string, src interface{}, opt *Options) (string, error)

GenProto translate kcl schema type to protobuf message.

Types

type Options

type Options struct {
	GoPackage string
	PbPackage string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL