Null Types, Safe primitive type conversion and fetching value from complex structures.

Overview

Typ

GoDoc Build Status Coverage Status Go Report Card Mentioned in Awesome Go

Typ is a library providing a powerful interface to impressive user experience with conversion and fetching data from built-in types in Golang

Features

  • Safe conversion along built-in types like as bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128, string
  • Null types for all primitive types with supported interfaces: json.Unmarshaler, json.Marshaler, sql.Scanner, driver.Valuer
  • Value retriever for multidimensional unstructured data from interface
  • Conversion functions present via interface (reflection) and native types for better performance
  • Some humanize string conversion functions

Installation

Use go get to install the latest version of the library.

    go get -u github.com/gurukami/typ

Then include package in your application and enjoy.

    import "github.com/gurukami/typ/v2"

Usage

Of(interface{}) conversion from interface value to built-in type

// typ.Of(v interface{}, options ...Option).{Type}(defaultValue ...{Type})
//
// Where {Type} any of 
//      Bool, 
//      Int, Int8, Int16, Int32, Int64, 
//      Uint, Uint8, Uint16, Uint32, Uint64, 
//      Float32, Float, 
//      Complex64, Complex, 
//      String
//
// All methods for conversion returns {Type}Accessor with helpful methods
//
//      V() - value of type
//      Present() - determines whether a value has been set
//      Valid() - determines whether a value has been valid (without error)
//      Err() error - returns underlying error  
//      Set(value {Type}) - saves value into current struct  
//      Clone() {Type}Accessor - returns new instance of current struct with preserved value & error  
//      
//      Scan(value interface{})         | sql.Scanner
//      Value() (driver.Value, error)   | driver.Valuer
//
//      UnmarshalJSON(b []byte) error   | json.Unmarshaler
//      MarshalJSON() ([]byte, error)   | json.Marshaler

// Valid
nv := typ.Of(3.1415926535, typ.FmtByte('g'), typ.Precision(4)).String()
fmt.Printf("Value: %v, Valid: %v, Present: %v, Error: %v\n", nv.V(), nv.Valid(), nv.Present(), nv.Error)
// Output: Value: 3.142, Valid: true, Present: true, Error: <nil>

// Not valid
nv = typ.Of(3.1415926535).Int()
fmt.Printf("Value: %v, Valid: %v, Present: %v, Error: %v\n", nv.V(), nv.Valid(), nv.Present(), nv.Error)
// Output: Value: 3, Valid: false, Present: true, Error: value can't safely convert

Native conversion without reflection when type is know

// For the best performance always use this way if you know about exact type
//
// typ.{FromType}{ToType}(value , [options ...{FromType}{ToType}Option]).V()
// 
// Where {FromType}, {ToType} any of 
//      Bool, 
//      Int, Int8, Int16, Int32, Int64, 
//      Uint, Uint8, Uint16, Uint32, Uint64, 
//      Float32, Float, 
//      Complex64, Complex, 
//      String
//
// All methods for conversion returns {Type}Accessor interface with helpful methods & fields, additional info you can read in example above

// Valid
nv := typ.FloatString(3.1415926535, typ.FloatStringFmtByte('g'), typ.FloatStringPrecision(4))
fmt.Printf("Value: %v, Valid: %v, Present: %v, Error: %v\n", nv.V(), nv.Valid(), nv.Present(), nv.Error)
// Output: Value: 3.142, Valid: true, Present: true, Error: <nil>

// Not valid
nv = typ.FloatInt(3.1415926535)
fmt.Printf("Value: %v, Valid: %v, Present: %v, Error: %v\n", nv.V(), nv.Valid(), nv.Present(), nv.Error)
// Output: Value: 3, Valid: false, Present: true, Error: value can't safely convert

Retrieve multidimensional unstructured data from interface

data := map[int]interface{}{
   0: []interface{}{
      0: map[string]int{
         "0": 42,
      },
   },
}

// Instead of do something like this 
//  data[0].([]interface{})[0].(map[string]int)["0”]
//      and not caught a panic
// use this

// Value exists
nv := typ.Of(data).Get(0, 0, "0").Interface()
fmt.Printf("Value: %v, Valid: %v, Present: %v, Error: %v\n", nv.V(), nv.Valid(), nv.Present(), nv.Error)
// Output: Value: 42, Valid: true, Present: true, Error: <nil>

// Value not exists
nv = typ.Of(data).Get(3, 7, "5").Interface()
fmt.Printf("Value: %v, Valid: %v, Present: %v, Error: %v\n", nv.V(), nv.Valid(), nv.Present(), nv.Error)
// Output: Value: <nil>, Valid: false, Present: false, Error: out of bounds on given data

Rules of safely type conversion along types

From / to Bool Int* String Uint* Float* Complex*
Bool + + + + + +
Int* + + formatting >= 0 24bit or 53bit real, 24bit or 53bit
String parsing parsing + parsing parsing parsing
Uint* + 63bit formatting + 24bit or 53bit 24bit or 53bit
Float* + 24bit or 53bit formatting >= 0, 24bit or 53bit + +
Complex* + real, 24bit or 53bit + >= 0, real, 24bit or 53bit real +

* based on bit size capacity, 8,16,32,64 for Int,Uint; 32,64 for Float,Complex

Donation for amazing goal

I like airplanes and i want to get private pilot licence, and i believe you can help me to make my dream come true :)

>>>>>>>>>> Make a dream come true <<<<<<<<<<

License

The MIT license
Copyright (c) 2019 Gurukami

Releases(v2.2.0)
Owner
Gurukami
Gurukami
Package iter provides generic, lazy iterators, functions for producing them from primitive types, as well as functions and methods for transforming and consuming them.

iter Package iter provides generic, lazy iterators, functions for producing them from primitive types, as well as functions and methods for transformi

Matthew Toohey 24 Jun 7, 2022
Go concurrent-safe, goroutine-safe, thread-safe queue

goconcurrentqueue - Concurrent safe queues The package goconcurrentqueue offers a public interface Queue with methods for a queue. It comes with multi

Enrique Bris 200 Aug 9, 2022
Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmarshallers

nan - No Allocations Nevermore Package nan - Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmar

Andrey Kuzmin 54 Jul 31, 2022
Type-safe, zero-allocation sets for Go

Set Package set is a type-safe, zero-allocation port of the excellent package fatih/set. It contains sets for most of the basic types and you can gene

ScyllaDB 795 Aug 10, 2022
A thread safe map which has expiring key-value pairs

~ timedmap ~ A map which has expiring key-value pairs. go get github.com/zekroTJA/timedmap Intro This package allows to set values to a map which will

zekro 44 Aug 8, 2022
Provides conversion from athena outputs to strongly-typed data models.

Provides conversion from athena outputs to strongly defined data models. Getting started Given the following data struct you define: type MyModel stru

Ken Timothy 4 Feb 7, 2022
Graph algorithms and data structures

Your basic graph Golang library of basic graph algorithms Topological ordering, image by David Eppstein, CC0 1.0. This library offers efficient and we

Algorithms to Go 569 Aug 13, 2022
Graph algorithms and data structures

Your basic graph Golang library of basic graph algorithms Topological ordering, image by David Eppstein, CC0 1.0. This library offers efficient and we

Algorithms to Go 9 Jan 25, 2021
Algorithms and Data Structures Solved in Golang

Algorithms and Data Structures Solved in Golang Hi! I'm Bruno Melo and this repository contains a lot challenges solved on many plataforms using go as

Bruno Melo 3 Aug 13, 2022
Some data structures and algorithms using golang

Some data structures and algorithms using golang

null 61 Aug 11, 2022
Data structures and algorithms implementation from ZeroToMastery course

ZeroToMastery Data Structures & Algorithms course This repo includes all the data structure and algorithm exercises solutions and implementations. Ins

Fabio Bozzo 3 Jul 4, 2022
Practice-dsa-go - Data Structures and Algorithms for Interview Preparation in Go

Data Structures and Algorithms for Interview Preparation in Go Data Structures K

Sparsh Srivastava 2 Jul 3, 2022
Implementation of various data structures and algorithms in Go

GoDS (Go Data Structures) Implementation of various data structures and algorithms in Go. Data Structures Containers Lists ArrayList SinglyLinkedList

TopXeQ 0 Jan 25, 2022
Tutorial code for my video Learn to Use Basic Data Structures - Slices, Structs and Maps in Golang

Learn to Use Basic Data Structures - Slices, Structs and Maps in Golang Read text from a file and split into words. Introduction to slices / lists. Co

null 0 Jan 26, 2022
Data Structures and Algorithms implementation in Go

Data Structures and Algorithms Clean and simple implementation in Go Implementation There are several data structures and algorithms implemented in th

Mykyta Paliienko 2.5k Aug 9, 2022
A tree like tool help you to explore data structures in your redis server

Redis-view is a tree like tool help you explore data structures in your redis server

dreamersdw 19 Mar 17, 2022
Probabilistic data structures for processing continuous, unbounded streams.

Boom Filters Boom Filters are probabilistic data structures for processing continuous, unbounded streams. This includes Stable Bloom Filters, Scalable

Tyler Treat 1.5k Aug 13, 2022
This is the course materials for the Go Data Structures Crash Course!

Go Data Structures Course ?? Welcome Gophers! This is the official repository that contains all of the data structures we cover in the Go Data Structu

TutorialEdge 9 May 10, 2022