• Stars
    star
    146
  • Rank 252,769 (Top 5 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 5 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Go implementation of Amazon Ion.

Amazon Ion Go

Build Status license docs

Amazon Ion ( https://amazon-ion.github.io/ion-docs/ ) library for Go

Ion Cookbook demonstrates code samples for some simple Amazon Ion use cases.

This package is based on work from David Murray (fernomac) on https://github.com/fernomac/ion-go. The Ion team greatly appreciates David's contributions to the Ion community.

Users

Here are some projects that use the Ion Go library

  • Restish: "...a CLI for interacting with REST-ish HTTP APIs with some nice features built-in"

We'll be happy to add you to our list, send us a pull request.

Git Setup

This repository contains a git submodule called ion-tests, which holds test data used by ion-go's unit tests.

The easiest way to clone the ion-go repository and initialize its ion-tests submodule is to run the following command.

$ git clone --recursive https://github.com/amazon-ion/ion-go.git ion-go

Alternatively, the submodule may be initialized independent of the clone by running the following commands:

$ git submodule init
$ git submodule update

Development

This package uses Go Modules to model its dependencies.

Assuming the go command is in your path, building the module can be done as:

$ go build -v ./...

Running all the tests can be executed with:

$ go test -v ./...

We use goimports to format our imports and files in general. Running this before commit is advised:

$ goimports -w .

It is recommended that you hook this in your favorite IDE (Tools > File Watchers in Goland, for example).

Usage

Import github.com/amazon-ion/ion-go/ion and you're off to the races.

Marshaling and Unmarshaling

Similar to GoLang's built-in json package, you can marshal and unmarshal Go types to Ion. Marshaling requires you to specify whether you'd like text or binary Ion. Unmarshaling is smart enough to do the right thing. Both follow the style of json name tags, and Marshal honors omitempty.

type T struct {
  A string
  B struct {
    RenamedC int   `ion:"C"`
    D        []int `ion:",omitempty"`
  }
}

func main() {
  t := T{}

  err := ion.Unmarshal([]byte(`{A:"Ion!",B:{C:2,D:[3,4]}}`), &t)
  if err != nil {
    panic(err)
  }
  fmt.Printf("--- t:\n%v\n\n", t)

  text, err := ion.MarshalText(&t)
  if err != nil {
    panic(err)
  }
  fmt.Printf("--- text:\n%s\n\n", string(text))

  binary, err := ion.MarshalBinary(&t)
  if err != nil {
    panic(err)
  }
  fmt.Printf("--- binary:\n%X\n\n", binary)
}

In order to Marshal/Unmarshal Ion values with annotation, we use a Go struct with two fields,

  1. one field of type []string and tagged with ion:",annotation".
  2. the other field with appropriate type and optional tag to hold our Ion value. For instance, to Marshal age::20, it must be in a struct as below:
  type foo struct {
    Value   interface{}
    AnyName []string `ion:",annotations"`
  }
  data := foo{20, []string{"age"}}
  val, err := ion.MarshalText(data)
  if err != nil {
     panic(err)
  }
  fmt.Println("Ion text: ", string(val)) // Ion text: age::20

And to Unmarshal the same data, we can do as shown below:

  type foo struct {
    Value   interface{}
    AnyName []string `ion:",annotations"`
  }
  var val foo
  err := ion.UnmarshalString("age::20", &val)
  if err != nil {
    panic(err)
  }
  fmt.Printf("Val = %+v\n", val) // Val = {Value:20 AnyName:[age]}

Encoding and Decoding

To read or write multiple values at once, use an Encoder or Decoder:

func main() {
  dec := ion.NewTextDecoder(os.Stdin)
  enc := ion.NewBinaryEncoder(os.Stdout)

  for {
    // Decode one Ion whole value from stdin.
    val, err := dec.Decode()
    if err == ion.ErrNoInput {
      break
    } else if err != nil {
      panic(err)
    }

    // Encode it to stdout.
    if err := enc.Encode(val); err != nil {
      panic(err)
    }
  }

  if err := enc.Finish(); err != nil {
    panic(err)
  }
}

Reading and Writing

For low-level streaming read and write access, use a Reader or Writer. The following example shows how to create a reader, read values from that reader, and write those values out using a writer:

func writeFromReaderToWriter(reader Reader, writer Writer) {
	for reader.Next() {
		name := reader.FieldName()
		if name != nil {
			err := writer.FieldName(*name)
			if err != nil {
				panic(err)
			}
		}

		an := reader.Annotations()
		if len(an) > 0 {
			err := writer.Annotations(an...)
			if err != nil {
				panic(err)
			}
		}

		currentType := reader.Type()
		if reader.IsNull() {
			err := writer.WriteNullType(currentType)
			if err != nil {
				panic(err)
			}
			continue
		}

		switch currentType {
		case BoolType:
			val, err := reader.BoolValue()
			if err != nil {
				panic("Something went wrong while reading a Boolean value: " + err.Error())
			}
			err = writer.WriteBool(val)
			if err != nil {
				panic("Something went wrong while writing a Boolean value: " + err.Error())
			}

		case StringType:
			val, err := reader.StringValue()
			if err != nil {
				panic("Something went wrong while reading a String value: " + err.Error())
			}
			err = writer.WriteString(val)
			if err != nil {
				panic("Something went wrong while writing a String value: " + err.Error())
			}

		case StructType:
			err := reader.StepIn()
			if err != nil {
				panic(err)
			}
			err = writer.BeginStruct()
			if err != nil {
				panic(err)
			}
			writeFromReaderToWriter(reader, writer)
			err = reader.StepOut()
			if err != nil {
				panic(err)
			}
			err = writer.EndStruct()
			if err != nil {
				panic(err)
			}
        default:
            panic("This is an example, only taking in Bool, String and Struct")
		}
	}

	if reader.Err() != nil {
		panic(reader.Err().Error())
	}
}

func main() {
	reader := NewReaderString("foo::{name:\"bar\", complete:false}")
	str := strings.Builder{}
	writer := NewTextWriter(&str)

	writeFromReaderToWriter(reader, writer)
	err := writer.Finish()
	if err != nil {
		panic(err)
	}
	fmt.Println(str.String())
}

Symbol Tables

By default, when writing binary Ion, a local symbol table is built as you write values (which are buffered in memory until you call Finish so the symbol table can be written out first). You can optionally provide one or more SharedSymbolTables to the writer, which it will reference as needed rather than directly including those symbols in the local symbol table.

type Item struct {
  ID          string    `ion:"id"`
  Name        string    `ion:"name"`
  Description string    `ion:"description"`
}

var ItemSharedSymbols = ion.NewSharedSymbolTable("item", 1, []string{
  "item",
  "id",
  "name",
  "description",
})

type SpicyItem struct {
  Item
  Spiciness   int       `ion:"spiciness"`
}

func WriteSpicyItemsTo(out io.Writer, items []SpicyItem) error {
  writer := ion.NewBinaryWriter(out, ItemSharedSymbols)

  for _, item := range items {
    writer.Annotation("item")
    if err := ion.EncodeTo(writer, item); err != nil {
      return err
    }
  }

  return writer.Finish()
}

You can alternatively provide the writer with a complete, pre-built local symbol table. This allows values to be written without buffering, however any attempt to write a symbol that is not included in the symbol table will result in an error:

func WriteItemsToLST(out io.Writer, items []SpicyItem) error {
  lst := ion.NewLocalSymbolTable([]SharedSymbolTable{ItemSharedSymbols}, []string{
    "spiciness",
  })

  writer := ion.NewBinaryWriterLST(out, lst)

  for _, item := range items {
    writer.Annotation("item")
    if err := ion.EncodeTo(writer, item); err != nil {
      return err
    }
  }

  return writer.Finish()
}

When reading binary Ion, shared symbol tables are provided by a Catalog. A basic catalog can be constructed by calling NewCatalog; a smarter implementation may load shared symbol tables from a database on demand.

func ReadItemsFrom(in io.Reader) ([]Item, error) {
  item := Item{}
  items := []Item{}

  cat := ion.NewCatalog(ItemSharedSymbols)
  dec := ion.NewDecoder(ion.NewReaderCat(in, cat))

  for {
    err := dec.DecodeTo(&item)
    if err == ion.ErrNoInput {
      return items, nil
    }
    if err != nil {
      return nil, err
    }

    items = append(items, item)
  }
}

License

This library is licensed under the Apache 2.0 License.

More Repositories

1

style-dictionary

A build system for creating cross-platform styles.
JavaScript
3,880
star
2

computer-vision-basics-in-microsoft-excel

Computer Vision Basics in Microsoft Excel (using just formulas)
2,394
star
3

selling-partner-api-docs

This repository contains documentation for developers to use to call Selling Partner APIs.
1,543
star
4

smoke-framework

A light-weight server-side service framework written in the Swift programming language.
Swift
1,443
star
5

alexa-skills-kit-js

SDK and example code for building voice-enabled skills for the Amazon Echo.
1,134
star
6

ion-java

Java streaming parser/serializer for Ion.
Java
840
star
7

selling-partner-api-models

This repository contains OpenAPI models for developers to use when developing software to call Selling Partner APIs.
Mustache
590
star
8

sketch-constructor

Read/write/manipulate Sketch files in Node without Sketch plugins!
JavaScript
542
star
9

pecos

PECOS - Prediction for Enormous and Correlated Spaces
Python
509
star
10

amzn-drivers

Official AWS drivers repository for Elastic Network Adapter (ENA) and Elastic Fabric Adapter (EFA)
C
455
star
11

ion-js

A JavaScript implementation of Amazon Ion.
TypeScript
323
star
12

convolutional-handwriting-gan

ScrabbleGAN: Semi-Supervised Varying Length Handwritten Text Generation (CVPR20)
Python
265
star
13

xfer

Transfer Learning library for Deep Neural Networks.
Python
253
star
14

awsssmchaosrunner

Amazon's light-weight library for chaos engineering on AWS. It can be used for EC2 and ECS (with EC2 launch type).
Kotlin
249
star
15

ion-python

A Python implementation of Amazon Ion.
Python
210
star
16

amazon-pay-sdk-php

Amazon Pay PHP SDK
PHP
209
star
17

kotlin-inject-anvil

Extensions for the kotlin-inject dependency injection framework
Kotlin
191
star
18

fire-app-builder

Fire App Builder is a framework for building java media apps for Fire TV, allowing you to add your feed of media content to a configuration file and build an app to browse and play it quickly.
Java
182
star
19

exoplayer-amazon-port

Official port of ExoPlayer for Amazon devices
Java
173
star
20

oss-dashboard

A dashboard for viewing many GitHub organizations at once.
Ruby
159
star
21

ion-c

A C implementation of Amazon Ion.
C
149
star
22

metalearn-leap

Original PyTorch implementation of the Leap meta-learner (https://arxiv.org/abs/1812.01054) along with code for running the Omniglot experiment presented in the paper.
Python
148
star
23

auction-gym

AuctionGym is a simulation environment that enables reproducible evaluation of bandit and reinforcement learning methods for online advertising auctions.
Jupyter Notebook
144
star
24

distance-assistant

Pedestrian monitor that provides visual feedback to help ensure proper social distancing guidelines are being observed
Python
135
star
25

hawktracer

HawkTracer is a highly portable, low-overhead, configurable profiling tool built in Amazon Video for getting performance metrics from low-end devices.
C++
133
star
26

trans-encoder

Trans-Encoder: Unsupervised sentence-pair modelling through self- and mutual-distillations
Python
133
star
27

smoke-aws

AWS services integration for the Smoke Framework
Swift
111
star
28

amazon-payments-magento-2-plugin

Extension to enable Amazon Pay on Magento 2
PHP
108
star
29

MXFusion

Modular Probabilistic Programming on MXNet
Python
103
star
30

amazon-weak-ner-needle

Named Entity Recognition with Small Strongly Labeled and Large Weakly Labeled Data
Python
100
star
31

amazon-advertising-api-php-sdk

⛔️ DEPRECATED - Amazon Advertising API PHP Client Library
PHP
93
star
32

ads-advanced-tools-docs

Code samples and supplements for the Amazon Ads advanced tools center
Jupyter Notebook
91
star
33

ion-rust

Rust implementation of Amazon Ion
Rust
86
star
34

image-to-recipe-transformers

Code for CVPR 2021 paper: Revamping Cross-Modal Recipe Retrieval with Hierarchical Transformers and Self-supervised Learning
Python
81
star
35

oss-attribution-builder

The OSS Attribution Builder is a website that helps teams create attribution documents (notices, "open source screens", credits, etc) commonly found in software products.
TypeScript
80
star
36

smoke-http

Specialised HTTP Client for service operations abstracted from the HTTP protocol.
Swift
70
star
37

amazon-ray

Staging area for ongoing enhancements to Ray focused on improving integration with AWS and other Amazon technologies.
Python
66
star
38

alexa-coho

Sample code for building skill adapters for Alexa Connected Home using the Lighting API
JavaScript
62
star
39

amazon-pay-sdk-ruby

Amazon Pay Ruby SDK
Ruby
58
star
40

selling-partner-api-samples

Sample code for Amazon Selling Partner API use cases
Java
56
star
41

amazon-pay-sdk-python

Amazon Pay Python SDK
Python
53
star
42

amazon-pay-sdk-java

Amazon Pay Java SDK
Java
53
star
43

zero-shot-rlhr

Python
51
star
44

supply-chain-simulation-environment

Python
50
star
45

amazon-pay-api-sdk-php

Amazon Pay API SDK (PHP)
PHP
48
star
46

amazon-pay-sdk-csharp

Amazon Pay C# SDK
C#
47
star
47

ion-dotnet

A .NET implementation of Amazon Ion.
C#
47
star
48

multiconer-baseline

Python
47
star
49

zeek-plugin-enip

Zeek network security monitor plugin that enables parsing of the Ethernet/IP and Common Industrial Protocol standards
Zeek
44
star
50

amazon-pay-sdk-samples

Amazon Pay SDK Sample Code
PHP
43
star
51

oss-contribution-tracker

Track contributions made to external projects and manage CLAs
TypeScript
40
star
52

amazon-s3-gst-plugin

A collection of Amazon S3 GStreamer elements.
C
40
star
53

fashion-attribute-disentanglement

Python
39
star
54

zeek-plugin-s7comm

Zeek network security monitor plugin that enables parsing of the S7 protocol
Zeek
39
star
55

milan

Milan is a Scala API and runtime infrastructure for building data-oriented systems, built on top of Apache Flink.
Scala
39
star
56

orthogonal-additive-gaussian-processes

Light-weighted code for Orthogonal Additive Gaussian Processes
Python
38
star
57

jekyll-doc-project

This repository contains an open-source Jekyll theme for authoring and publishing technical documentation. This theme is used by Appstore/Alexa tech writers and other community members. Most of the theme's files are stored in a Ruby Gem (called jekyll-doc-project).
HTML
37
star
58

amazon-pay-api-sdk-nodejs

Amazon Pay API SDK (Node.js)
JavaScript
36
star
59

smoke-dynamodb

SmokeDynamoDB is a library to make it easy to use DynamoDB from Swift-based applications, with a particular focus on usage with polymorphic database tables (tables that do not have a single schema for all rows).
Swift
34
star
60

chalet-charging-location-for-electric-trucks

Optimization tool to identify charging locations for electric trucks
Python
34
star
61

sparse-vqvae

Experimental implementation for a sparse-dictionary based version of the VQ-VAE2 paper
Python
31
star
62

ss-aga-kgc

Python
31
star
63

amazon-pay-api-sdk-java

Amazon Pay API SDK (Java)
Java
30
star
64

zeek-plugin-bacnet

Zeek network security monitor plugin that enables parsing of the BACnet standard building controls protocol
Zeek
29
star
65

credence-to-causal-estimation

A framework for generating complex and realistic datasets for use in evaluating causal inference methods.
Python
29
star
66

basis-point-sets

Python
28
star
67

buy-with-prime-cdk-constructs

This package extends common CDK constructs with opinionated defaults to help create an organization strategy around infrastructure as code.
TypeScript
28
star
68

zeek-plugin-profinet

Zeek network security monitor plugin that enables parsing of the Profinet protocol
Zeek
27
star
69

differential-privacy-bayesian-optimization

This repo contains the underlying code for all the experiments from the paper: "Automatic Discovery of Privacy-Utility Pareto Fronts"
Python
26
star
70

ion-tests

Test vectors for testing compliant Ion implementations.
25
star
71

ion-hive-serde

A Apache Hive SerDe (short for serializer/deserializer) for the Ion file format.
Java
24
star
72

zeek-plugin-tds

Zeek network security monitor plugin that enables parsing of the Tabular Data Stream (TDS) protocol
Zeek
24
star
73

smoke-framework-application-generate

Code generator to generate SmokeFramework-based applications from service models.
Swift
24
star
74

ion-intellij-plugin

Support for Ion in Intellij IDEA.
Kotlin
23
star
75

ion-schema-kotlin

A Kotlin reference implementation of the Ion Schema Specification.
Kotlin
23
star
76

ftv-livetv-sample-tv-app

Java
23
star
77

emukit-playground

A web page explaining concepts of statistical emulation and making decisions under uncertainty in an interactive way.
JavaScript
22
star
78

ion-hash-go

A Go implementation of Amazon Ion Hash.
Go
22
star
79

pretraining-or-self-training

Codebase for the paper "Rethinking Semi-supervised Learning with Language Models"
Python
22
star
80

smoke-framework-examples

Sample applications showing the usage of the SmokeFramework and related libraries.
Swift
21
star
81

confident-sinkhorn-allocation

Pseudo-labeling for tabular data
Jupyter Notebook
21
star
82

tiny-attribution-generator

A small tool and library to create attribution notices from various formats
TypeScript
20
star
83

smoke-aws-generate

Code generator to generate the SmokeAWS library from service models.
Swift
19
star
84

ion-docs

Source for the GitHub Pages for Ion.
Java
19
star
85

autotrail

AutoTrail is a highly modular, partial automation workflow engine providing run time execution control
Python
19
star
86

smoke-aws-credentials

A library to obtain and assume automatically rotating AWS IAM roles written in the Swift programming language.
Swift
19
star
87

amazon-codeguru-profiler-for-spark

A Spark plugin for CPU and memory profiling
Java
18
star
88

git-commit-template

Set commit templates for git
JavaScript
18
star
89

service-model-swift-code-generate

Modular code generator to generate Swift applications from service models.
Swift
18
star
90

amazon-pay-api-sdk-dotnet

Amazon Pay API SDK (.NET)
C#
18
star
91

sample-fire-tv-app-video-skill

This sample Fire TV app shows how to integrate an Alexa video skill in a simple, basic way.
Java
16
star
92

amazon-template-library

A collection of general purpose C++ utilities that play well with the Standard Library and Boost.
C++
16
star
93

rheoceros

Cloud-based AI / ML workflow and data application development framework
Python
16
star
94

ion-cli

Rust
15
star
95

refuel-open-domain-qa

Python
15
star
96

amazon-instant-access-sdk-php

PHP SDK to aid in 3p integration with Instant Access
PHP
14
star
97

amazon-mcf-plugin-for-magento-1

Plugin code to enable Amazon MCF in Magento 1.
PHP
14
star
98

login-with-amazon-wordpress

A pre-integrated plugin that can be installed into a Wordpress powered website to integrate with Login with Amazon.
PHP
14
star
99

firetv-sample-touch-app

This sample Android project demonstrates how to build the main UI of a Fire TV application in order to support both Touch interactions and Remote D-Pad controls.
Java
14
star
100

amzn-ec2-ena-utilities

Python
14
star