generate

package
v0.0.4-memsql Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package gen provides generated go structures based on a given SCIM schema.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomType

type CustomType struct {
	PkgPrefix string // package id
	AttrName  string // name of the attribute
	TypeName  string // name of the custom type
}

type StructGenerator

type StructGenerator struct {
	// contains filtered or unexported fields
}

func NewStructGenerator

func NewStructGenerator(s schema.ReferenceSchema, extensions ...schema.ReferenceSchema) (StructGenerator, error)

func (*StructGenerator) AddTags

func (g *StructGenerator) AddTags(f func(a *schema.Attribute) (tags map[string]string)) *StructGenerator

AddTags enables setting fields tags when the attribute is has certain attribute fields such as required.

Example
package main

import (
	"fmt"

	"github.com/memsql/scimtools/schema"

	"github.com/memsql/scimtools/generate"
)

func main() {
	g, _ := generate.NewStructGenerator(schema.ReferenceSchema{
		Name:        "User",
		Description: "User Account",
	})
	g.AddTags(func(a *schema.Attribute) (tags map[string]string) {
		tags = make(map[string]string)
		if a.Required {
			tags["x"] = "required"
		}
		if a.Uniqueness == schema.Server {
			x, ok := tags["x"]
			if !ok {
				tags["x"] = "unique"
			} else {
				tags["x"] = x + ",unique"
			}
		}
		return tags
	})
	fmt.Print(g.Generate())

}
Output:

// User Account
type User struct {
    ExternalID string
    ID         string `x:"required,unique"`
}

func (*StructGenerator) CustomTypes

func (g *StructGenerator) CustomTypes(types []CustomType) *StructGenerator

CustomTypes allows defining custom types for an attribute.

Example
package main

import (
	"fmt"

	"github.com/memsql/scimtools/schema"

	"github.com/memsql/scimtools/generate"
)

func main() {
	ref := schema.ReferenceSchema{
		Name:       "User",
		Attributes: []*schema.Attribute{schema.MetaAttribute},
	}
	g, _ := generate.NewStructGenerator(ref)
	g.CustomTypes([]generate.CustomType{
		{
			PkgPrefix: "uuid",
			AttrName:  "id",
			TypeName:  "UUID",
		},
		{
			AttrName: "meta",
			TypeName: "Meta",
		},
	})
	fmt.Print(g.Generate())

}
Output:

type User struct {
    ExternalID string
    ID         uuid.UUID
    Meta       Meta
}

func (*StructGenerator) Generate

func (g *StructGenerator) Generate() *bytes.Buffer

Generate creates a buffer with a go representation of the resource described in the given schema.

Example (Empty)
package main

import (
	"fmt"

	"github.com/memsql/scimtools/schema"

	"github.com/memsql/scimtools/generate"
)

func main() {
	g, _ := generate.NewStructGenerator(schema.ReferenceSchema{
		Name:        "User",
		Description: "User Account",
	})
	fmt.Print(g.Generate())

}
Output:

// User Account
type User struct {
    ExternalID string
    ID         string
}
Example (Extensions)
package main

import (
	"fmt"

	"github.com/memsql/scimtools/schema"

	"github.com/memsql/scimtools/generate"
)

func main() {
	g, _ := generate.NewStructGenerator(
		schema.ReferenceSchema{
			Name: "User",
			Attributes: []*schema.Attribute{
				{
					Name:       "userName",
					Required:   true,
					Uniqueness: schema.Server,
				},
			},
		},
		schema.ReferenceSchema{
			ID:   "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",
			Name: "Enterprise User",
			Attributes: []*schema.Attribute{
				{Name: "employeeNumber"},
			},
		},
	)
	fmt.Print(g.Generate())

}
Output:

type User struct {
    ExternalID string
    ID         string
    UserName   string

    EnterpriseUser EnterpriseUserExtension `scim:"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"`
}

type EnterpriseUserExtension struct {
    EmployeeNumber string
}
Example (Minimal)
package main

import (
	"fmt"

	"github.com/memsql/scimtools/schema"

	"github.com/memsql/scimtools/generate"
)

func main() {
	g, _ := generate.NewStructGenerator(schema.ReferenceSchema{
		Name: "User",
		Attributes: []*schema.Attribute{
			{
				Name:       "userName",
				Required:   true,
				Uniqueness: schema.Server,
			},
		},
	})
	fmt.Print(g.Generate())

}
Output:

type User struct {
    ExternalID string
    ID         string
    UserName   string
}

func (*StructGenerator) UsePtr

func (g *StructGenerator) UsePtr(t bool) *StructGenerator

UsePtr indicates whether the generator will use pointers if the attribute is not required.

Jump to

Keyboard shortcuts

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