csharp_json

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2022 License: MulanPSL-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TmplDefineST = &generator.TemplateHeader{
	Name:     "csharp_json_define_st",
	Filepath: "csharp_json_define_st.tmpl",

	BuiltinTemplateText: `
{{if (ne .DocComment "")}}    {{.DocComment}}
{{end}}    public class {{.Name}} : {{.BaseName}}
    {
{{range .Fields}}{{if (ne .DocComment "")}}        {{.DocComment}}
{{end}}{{if (ne .JsonName "")}}        [JsonPropertyName("{{.JsonName}}")]
{{end}}        public {{.TypeStr}} {{.Name}} {get;set;}{{if (ne .LineComment "")}} {{.LineComment}}{{end}}
{{end}}
        public {{.Name}}()
        {
{{range .Fields}}{{if (ne .ShouldInitCustomType "")}}            {{.Name}} = new {{.ShouldInitCustomType}}();
{{end}}{{if .ShouldInitString}}            {{.Name}} = "";
{{end}}{{end}}        }
        public override string Name()
        {
            return "{{.Name}}";
        }
        public override {{.Name}} NewST()
        {
            return new {{.Name}}();
        }
        public override Byte[] Encode()
        {
            return JsonSerializer.SerializeToUtf8Bytes(this);
        }
        public override void Decode(Byte[] bs)
        {
            {{.Name}}? obj = JsonSerializer.Deserialize<{{.Name}}>(bs);
            if (obj == null)
            {
                throw new Exception("deserialize {{.Name}} failed");
            }
{{range .Fields}}            this.{{.Name}} = obj.{{.Name}};
{{end}}        }
        public override string ToString()
		{
            return JsonSerializer.Serialize(this);
        }
{{if .IsMessage}}
        public override UInt32 ProtoID()
        {
            return MessageDef.ProtoID{{.Name}};
        }
        public override {{.Name}} New()
        {
            return NewST();
        }
{{end}}
    }
`,
}
View Source
var TmplProtoFile = &generator.TemplateHeader{
	Name:     "csharp_json_proto_file",
	Filepath: "csharp_json_proto_file.tmpl",

	BuiltinTemplateText: `using System;
using System.Collections.Generic;
{{if (len .DefineSTCode | ne 0)}}using System.Text.Json;
using System.Text.Json.Serialization;
{{end}}
namespace {{.Namespace}}
{
{{range .DefineSTCode}}{{.}}{{end}}
}
`,
}

Functions

func GenCodeForDefineST

func GenCodeForDefineST(pSet *generator.PrototypeSet, df *generator.DefineST, isMessage bool) (string, error)

func GenFileForMessage

func GenFileForMessage(pSet *generator.PrototypeSet, origFn string) error

func GenFileForSet

func GenFileForSet(pSet *generator.PrototypeSet) error

Types

type Generator

type Generator struct {
}

func (*Generator) Generate

func (*Generator) Lang

func (g *Generator) Lang() string

func (*Generator) NewConfig

func (g *Generator) NewConfig() generator.GeneratorConfig

func (*Generator) Proto

func (g *Generator) Proto() string

type GeneratorConfig

type GeneratorConfig struct {
	Dir           string `json:"dir,omitempty"`
	NamespaceName string `json:"namespace_name,omitempty"`

	MessageFilename string `json:"message_filename,omitempty"`
	ConstFilename   string `json:"const_filename,omitempty"`

	Plugins []string `json:"plugins,omitempty"`
}

type TmplDefineSTData added in v0.2.0

type TmplDefineSTData struct {
	Name     string
	BaseName string

	DocComment string
	Fields     []*TmplDefineSTDataField

	IsMessage bool

	Plugins map[string]interface{}
}

type TmplDefineSTDataField added in v0.2.0

type TmplDefineSTDataField struct {
	Name        string
	TypeStr     string
	DocComment  string
	LineComment string

	JsonName string

	ShouldInitCustomType string
	ShouldInitString     bool

	Plugins map[string]interface{}
}

type TmplProtoFileData added in v0.2.0

type TmplProtoFileData struct {
	Import    []string
	Namespace string

	DefineSTCode []string

	Plugins map[string]interface{}
}

Jump to

Keyboard shortcuts

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