c3po

package module
v0.0.0-...-a2f0a7e Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: BSD-3-Clause Imports: 9 Imported by: 2

README

c3po

A GoLang data validator

	ParseSchema(struct{}) => struct{}
	ParseSchema(&struct{}) => *struct{}
	ParseSchema(&struct{Field:Value}) => *struct{Field: value} // with default value

	type Schema struct{
		Field `c3po:"-"` // omit this field
		Field `c3po:"realName"` // string: real name field
		Field `c3po:"name"` 	// string: name of validation	(default realName)
		Field `c3po:"escape"`	// bool: escape html value		(default false)
		Field `c3po:"required"` // bool:		...			 	(default false)
		Field `c3po:"nullable"` // bool:if true,allow nil value (default false)
		Field `c3po:"recursive"`// bool: deep validation	  	(default true)
		Field `c3po:"skiponerr"`// bool: omit on valid. error 	(default false)
	}

Basic Usage

package main

import (
 "encoding/json"
 "fmt"

 "github.com/ethoDomingues/c3po"
)

type Model struct {
 UUID string
}
type User struct {
 Model `c3po:"heritage"`
 Name  string `c3po:"name=username"`
 Email string `c3po:"required"`
}

type Studant struct {
 User
 Curse *Curse `c3po:"recursive=false"`
}

type Curse struct {
 Name     string
 Duration string
}

func main() {
 var userData = map[string]any{
  "uuid":  "aaaa-aaaa-aaaa", // here 'uuid' is inheritance from 'model'
  "email": "tião@localhost",
 }

 var studantData = map[string]any{
  "user": userData,
  "curse": &Curse{
   Name:     "mechanic",
   Duration: "infinity",
  },
 }

 u := &User{Name: "tião"} // here the 'name' field has a default value
 userSchema := c3po.ParseSchema(u)
 u2, _ := userSchema.Decode(userData)
 fmt.Println(c3po.EncodeToString(u))
 fmt.Println(c3po.EncodeToString(u2))

 s := &Studant{}
 studantSchema := c3po.ParseSchema(s)
 s2, _ := studantSchema.Decode(studantData)
 fmt.Println(c3po.EncodeToString(s))
 fmt.Println(c3po.EncodeToString(s2))
}

Output:

$ go run .
{
 "UUID": "",
 "Name": "tião",
 "Email": ""
}
{
 "UUID": "aaaa-aaaa-aaaa",
 "Name": "tião",
 "Email": "tião@localhost"
}
{
 "UUID": "",
 "Name": "",
 "Email": "",
 "Curse": null
}
{
 "UUID": "aaaa-aaaa-aaaa",
 "Name": "",
 "Email": "tião@localhost",
 "Curse": {
  "Name": "mechanic",
  "Duration": "infinit"
}

Others Functions

	c3po.Encode(struct{Name:"Jão", Age:99}) => map[string]any{"Name":"jão", "Age":99}
	c3po.Encode([]struct{Name:"Jão", Age:99}) => []map[string]any{"Name":"jão", "Age":99}

  
	c3po.EncodeToJSON(struct{Name:"Jão", Age:99}) => []byte{"{'Name':'jão', 'Age':99}"}
	c3po.EncodeToString(struct{Name:"Jão", Age:99}) => "{'Name':'jão', 'Age':99}"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(v ...any) (any, error)

Transforms a complex struct into a map or a []map

c3po.Encode(struct{Name:"Jão"}) => map[string]any{"Name":"jão"}
c3po.Encode(struct{Name:"Jão", Age:99}) => map[string]any{"Name":"jão", "Age":99}
c3po.Encode([]struct{Name:"Jão", Age:99}) => []map[string]any{"Name":"jão", "Age":99}

func EncodeToJSON

func EncodeToJSON(v ...any) ([]byte, error)

EncodeToJSON is similar to Encode, but return a []byte{}

c3po.EncodeToJSON(struct{Name:"Jão", Age:99}) => []byte{"{'Name':'jão', 'Age':99}"}

func EncodeToString

func EncodeToString(v ...any) (string, error)

EncodeToString is similar to EncodeToJSON, but return a []byte{}

c3po.EncodeToString(struct{Name:"Jão", Age:99}) => "{'Name':'jão', 'Age':99}"

func GetFunctionName

func GetFunctionName(i interface{}) string

func GetReflectElem

func GetReflectElem(r reflect.Value) reflect.Value

func GetReflectTypeElem

func GetReflectTypeElem(t reflect.Type) reflect.Type

func HtmlEscape

func HtmlEscape(s string) string

func RetInvalidType

func RetInvalidType(f *Fielder) error

func RetMissing

func RetMissing(f *Fielder) error

func SetReflectValue

func SetReflectValue(r reflect.Value, v reflect.Value, escape bool) bool

Types

type Fielder

type Fielder struct {
	Name     string
	RealName string

	Default any
	Schema  any

	Escape    bool // default: false
	Required  bool // default: false
	Nullable  bool // default: false
	Heritage  bool // default: false
	Recursive bool // default: true
	SkipOnErr bool // default: false

	IsMAP,
	IsSlice,
	IsStruct,
	IsPointer bool

	SliceType,
	MapKeyType,
	MapValueType *Fielder

	Type reflect.Kind
	Tags map[string]string

	SuperIndex *int // if a field to a struct

	Children      map[string]*Fielder
	FieldsByIndex map[int]string
}

func ParseSchema

func ParseSchema(schema any) *Fielder

usage:

c3po.ParseSchema(struct{}) => struct{}
c3po.ParseSchema(&struct{}) => *struct{}
c3po.ParseSchema(&struct{Field:Value}) => *struct{Field: value} // with default value

type Schema struct{
	Field `c3po:"-"` // omit this field
	Field `c3po:"realName"` // string: real name field
	Field `c3po:"name"` 	// string: name of validation	(default realName)
	Field `c3po:"escape"`	// bool: escape html value		(default false)
	Field `c3po:"required"` // bool:		...			 	(default false)
	Field `c3po:"nullable"` // bool: if true, allow nil value (default false)
	Field `c3po:"recursive"`// bool: deep validation	  	(default true)
	Field `c3po:"skiponerr"`// bool: omit on valid. error 	(default false)
}

func ParseSchemaWithTag

func ParseSchemaWithTag(tagKey string, schema any) *Fielder

func (*Fielder) CheckSchPtr

func (f *Fielder) CheckSchPtr(r reflect.Value) any

func (*Fielder) Decode

func (f *Fielder) Decode(data any) (any, error)

func (*Fielder) New

func (f *Fielder) New() reflect.Value

Jump to

Keyboard shortcuts

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