schema

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2017 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const (
	IntegerType   = "integer"
	StringType    = "string"
	BooleanType   = "boolean"
	NumberType    = "number"
	DateType      = "date"
	ObjectType    = "object"
	ArrayType     = "array"
	DateTimeType  = "datetime"
	TimeType      = "time"
	YearMonthType = "yearmonth"
	YearType      = "year"
	DurationType  = "duration"
	GeoPointType  = "geopoint"
)

Field types.

View Source
const (
	GeoPointArrayFormat  = "array"
	GeoPointObjectFormat = "object"
)

Formats specific to GeoPoint field type.

View Source
const (
	AnyDateFormat = "any"
)

Formats.

View Source
const InvalidPosition = -1

InvalidPosition is returned by GetField call when it refers to a field that does not exist in the schema.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	// Name of the field. It is mandatory and shuold correspond to the name of field/column in the data file (if it has a name).
	Name   string `json:"name"`
	Type   string `json:"type,omitempty"`
	Format string `json:"format,omitempty"`
	// A human readable label or title for the field.
	Title string `json:"title,omitempty"`
	// A description for this field e.g. "The recipient of the funds"
	Description string `json:"description,omitempty"`

	// Boolean properties. Define set of the values that represent true and false, respectively.
	// https://specs.frictionlessdata.io/table-schema/#boolean
	TrueValues  []string `json:"trueValues,omitempty"`
	FalseValues []string `json:"falseValues,omitempty"`
}

JSON object that describes a single field. More: https://specs.frictionlessdata.io/table-schema/#field-descriptors

func (*Field) CastValue

func (f *Field) CastValue(value string) (interface{}, error)

CastValue casts a value against field. Returns an error if the value can not be cast or any field constraint can no be satisfied.

func (*Field) TestValue

func (f *Field) TestValue(value string) bool

TestValue checks whether the value can be casted against the field.

func (*Field) UnmarshalJSON

func (f *Field) UnmarshalJSON(data []byte) error

UnmarshalJSON sets *f to a copy of data. It will respect the default values described at: https://specs.frictionlessdata.io/table-schema/

type Fields

type Fields []Field

Field represents a list of schema fields.

func (Fields) Len

func (f Fields) Len() int

func (Fields) Less

func (f Fields) Less(i, j int) bool

func (Fields) Swap

func (f Fields) Swap(i, j int)

type ForeignKeyReference

type ForeignKeyReference struct {
	Resource          string      `json:"resource,omitempty"`
	Fields            []string    `json:"-"`
	FieldsPlaceholder interface{} `json:"fields,omitempty"`
}

type ForeignKeys

type ForeignKeys struct {
	Fields            []string            `json:"-"`
	FieldsPlaceholder interface{}         `json:"fields,omitempty"`
	Reference         ForeignKeyReference `json:"reference,omitempty"`
}

ForeignKeys defines a schema foreign key

type GeoPoint

type GeoPoint struct {
	Lon float64 `json:"lon,omitempty"`
	Lat float64 `json:"lat,omitempty"`
}

GeoPoint represents a "geopoint" cell. More at: https://specs.frictionlessdata.io/table-schema/#geopoint

func (*GeoPoint) UnmarshalJSON

func (p *GeoPoint) UnmarshalJSON(data []byte) error

UnmarshalJSON sets *f to a copy of data. It will respect the default values

type Schema

type Schema struct {
	Fields                Fields      `json:"fields,omitempty"`
	PrimaryKeyPlaceholder interface{} `json:"primaryKey,omitempty"`
	PrimaryKeys           []string    `json:"-"`
	ForeignKeys           ForeignKeys `json:"foreignKeys,omitempty"`
	MissingValues         []string    `json:"missingValues,omitempty"`
}

Schema describes tabular data.

func Infer

func Infer(headers []string, table [][]string) (*Schema, error)

Infer infers a schema from a slice of the tabular data. For columns that contain cells that can inferred as different types, the most popular type is set as the field type. For instance, a column with values 10.1, 10, 10 will inferred as being of type "integer".

Example
headers := []string{"Person", "Height"}
table := [][]string{
	[]string{"Foo", "5"},
	[]string{"Bar", "4"},
	[]string{"Bez", "5.5"},
}
s, _ := Infer(headers, table)
fmt.Println("Fields:")
for _, f := range s.Fields {
	fmt.Printf("{Name:%s Type:%s Format:%s}\n", f.Name, f.Type, f.Format)
}
Output:

Fields:
{Name:Person Type:string Format:default}
{Name:Height Type:integer Format:default}

func InferImplicitCasting

func InferImplicitCasting(headers []string, table [][]string) (*Schema, error)

InferImplicitCasting uses a implicit casting for infering the type of columns that have cells of diference types. For instance, a column with values 10.1, 10, 10 will inferred as being of type "number" ("integer" can be implicitly cast to "number").

For medium to big tables, this method is faster than the Infer.

Example
headers := []string{"Person", "Height"}
table := [][]string{
	[]string{"Foo", "5"},
	[]string{"Bar", "4"},
	[]string{"Bez", "5.5"},
}
s, _ := InferImplicitCasting(headers, table)
fmt.Println("Fields:")
for _, f := range s.Fields {
	fmt.Printf("{Name:%s Type:%s Format:%s}\n", f.Name, f.Type, f.Format)
}
Output:

Fields:
{Name:Person Type:string Format:default}
{Name:Height Type:number Format:default}

func Read

func Read(r io.Reader) (*Schema, error)

Read reads and parses a descriptor to create a schema.

Example - Reading a schema from a file:

f, err := os.Open("foo/bar/schema.json")
if err != nil {
  panic(err)
}
s, err := Read(f)
if err != nil {
  panic(err)
}
fmt.Println(s)

func ReadFromFile

func ReadFromFile(path string) (*Schema, error)

ReadFromFile reads and parses a schema descrptor from a local file.

func (*Schema) CastRow

func (s *Schema) CastRow(row []string, out interface{}) error

CastRow casts a row to schema types. The out value must be pointer to a struct. Only exported fields will be cast. The lowercased field name is used as the key for each exported field.

If a value in the row cannot be cast to its respective schema field (Field.CastValue), this call will return an error. Furthermore, this call is also going to return an error if the schema field value can not be cast to the struct field type.

func (*Schema) GetField

func (s *Schema) GetField(name string) (*Field, int)

GetField fetches the index and field referenced by the name argument.

func (*Schema) HasField

func (s *Schema) HasField(name string) bool

HasField returns checks whether the schema has a field with the passed-in.

func (*Schema) Headers

func (s *Schema) Headers() []string

Headers returns the headers of the tabular data described by the schema.

func (*Schema) MarshalJSON

func (s *Schema) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of s.

func (*Schema) SaveToFile

func (s *Schema) SaveToFile(path string) error

SaveToFile writes the schema descriptor in local file.

func (*Schema) UnmarshalJSON

func (s *Schema) UnmarshalJSON(data []byte) error

UnmarshalJSON sets *f to a copy of data. It will respect the default values described at: https://specs.frictionlessdata.io/table-schema/

func (*Schema) Validate

func (s *Schema) Validate() error

Validate checks whether the schema is valid. If it is not, returns an error describing the problem. More at: https://specs.frictionlessdata.io/table-schema/

func (*Schema) Write

func (s *Schema) Write(w io.Writer) error

Write writes the schema descriptor.

Jump to

Keyboard shortcuts

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