darknut

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2023 License: MIT Imports: 9 Imported by: 1

README

Darknut

GoDev Build Status

Image

Darknut can be used to unmarshal a NewsDoc into a specialised struct. Mostly useful for getting strict typing of data attributes and flattening the data structure.

Slices of blocks and fields that are pointers will be treated as optional, all others will result in an error. External types, like UUIDs, are supported through TextUnmarshaler.

type planningItem struct {
	UUID                uuid.UUID         `newsdoc:"uuid"`
	Title               string            `newsdoc:"title"`
	Meta                planningItemBlock `newsdoc:"meta,type=core/planning-item"`
    InternalDescription *descriptionBlock `newsdoc:"meta,type=core/description,role=internal"`
	PublicDescription   *descriptionBlock `newsdoc:"meta,type=core/description,role=public"`
	Assignments         []assignmentBlock `newsdoc:"meta,type=core/assignment"`
}

type descriptionBlock struct {
	Role string `newsdoc:"role"`
	Text string `newsdoc:"data.text"`
}

type planningItemBlock struct {
	Date        *time.Time `newsdoc:"data.date,format=2006-01-02"`
	Publish     time.Time  `newsdoc:"data.publish"`
	PublishSlot *int       `newsdoc:"data.publish_slot"`
	Public      bool       `newsdoc:"data.public"`
	Tentative   bool       `newsdoc:"data.tentative"`
	Urgency     int        `newsdoc:"data.urgency"`
}

type assignmentBlock struct {
	Starts    time.Time        `newsdoc:"data.starts"`
	Ends      *time.Time       `newsdoc:"data.ends"`
	Status    string           `newsdoc:"data.status"`
	FullDay   bool             `newsdoc:"data.full_day"`
	Kind      []assignmentKind `newsdoc:"meta,type=core/assignment-kind"`
	Assignees []assigneeLink   `newsdoc:"links,rel=assignee"`
}

type assignmentKind struct {
	Value string `newsdoc:"value"`
}

type assigneeLink struct {
	UUID uuid.UUID `newsdoc:"uuid"`
}

See the documentation for more information.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnmarshalDocument

func UnmarshalDocument(doc newsdoc.Document, o any) error

UnmarshalDocument unmarshals a NewsDoc document into a struct.

Example
package main

import (
	"fmt"
	"time"

	"github.com/google/uuid"
	"github.com/ttab/darknut"
	"github.com/ttab/newsdoc"
)

type exampleItem struct {
	UUID       uuid.UUID     `newsdoc:"uuid"`
	Meta       exampleBlock  `newsdoc:"meta,type=example/meta"`
	Associates []exampleLink `newsdoc:"links,rel=associate"`
}

type exampleBlock struct {
	Date      *time.Time `newsdoc:"data.date,format=2006-01-02"`
	Timestamp time.Time  `newsdoc:"data.timestamp"`
	Public    bool       `newsdoc:"data.public"`
}

type exampleLink struct {
	Type string `newsdoc:"type"`
	Name string `newsdoc:"name"`
	Age  *int   `newsdoc:"data.age"`
}

func main() {
	doc := newsdoc.Document{
		UUID: "d04e9871-c3df-4fb0-878f-23f8d5ada7c2",
		Meta: []newsdoc.Block{
			{
				Type: "example/meta",
				Data: newsdoc.DataMap{
					"date":      "2023-08-13",
					"timestamp": "2023-08-21T14:34:47+02:00",
					"public":    "true",
				},
			},
		},
		Links: []newsdoc.Block{
			{
				Rel:  "associate",
				Name: "Ren",
				Type: "dog/chihuahua",
			},
			{
				Rel:  "associate",
				Name: "Stimpy",
				Type: "cat/manx",
				Data: newsdoc.DataMap{"age": "3"},
			},
		},
	}

	var item exampleItem

	err := darknut.UnmarshalDocument(doc, &item)
	if err != nil {
		panic(err)
	}

	fmt.Printf(`
UUID: %v
Date: %v
Timestamp: %v
Public: %v

`,
		item.UUID, item.Meta.Date.Format(time.RFC3339),
		item.Meta.Timestamp.Format(time.RFC3339), item.Meta.Public)

	for _, a := range item.Associates {
		fmt.Printf("* %s (%s)", a.Name, a.Type)

		if a.Age != nil {
			fmt.Printf(" age: %v", *a.Age)
		}

		fmt.Println()
	}

}
Output:

UUID: d04e9871-c3df-4fb0-878f-23f8d5ada7c2
Date: 2023-08-13T00:00:00Z
Timestamp: 2023-08-21T14:34:47+02:00
Public: true

* Ren (dog/chihuahua)
* Stimpy (cat/manx) age: 3

Types

This section is empty.

Jump to

Keyboard shortcuts

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