text:
<?php

namespace Thorazine\Hack\Http\Controllers\Cms;

use App\Http\Controllers\Controller;
use Thorazine\Hack\Http\Requests\BuilderUpdate;
use Thorazine\Hack\Http\Requests\BuilderStore;
use Illuminate\Http\Request;
use Thorazine\Hack\Traits\ModuleSearch;
use Thorazine\Hack\Traits\ModuleHelper;
use Thorazine\Hack\Models\Templateable;
use Thorazine\Hack\Models\Template;
use Thorazine\Hack\Models\Pageable;
use Thorazine\Hack\Http\Requests;
use Thorazine\Hack\Models\DbLog;
use Thorazine\Hack\Models\Page;
use Exception;
use Builder;
use Cms;
use Log;
use DB;

class BuilderController
{
    use ModuleHelper;

    use ModuleSearch;

    public function __construct(Template $model, Templateable $templateable, Page $page, Pageable $pageable)
    {
        $this->model = $model;
        $this->templateable = $templateable;
        $this->pageable = $pageable;
        $this->page = $page;
        $this->slug = 'builder';

        $this->model->types = $templateable->types;

        // parent::__construct($this);

        // set the create route
        $this->types = $this->model->types;

        view()->share([
            'hasOrder' => true,
            'createRoute' => 'cms.'.$this->slug.'.module',
            'slug' => $this->slug,
            'types' => $this->types,
            'model' => $this->model,
            'hasPermission' => function($action) {
                return Cms::hasPermission(Cms::siteId().'.cms.'.$this->slug.'.'.$action);
            },
            'route' => function($action) {
                return 'cms.'.$this->slug.'.'.$action;
            },
            'typeTrue' => function($type, $field, $default = true) {
                // if not excists, true
                if(array_key_exists($field, $type)) {
                    if(! $type[$field]) {
                        return false;
                    }
                    else {
                        return true;
                    }
                }
                return $default;
            },
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // get builders
        $datas = Builder::getTemplateBuilders($request->fid, $this->model, false);

        if($request->ajax()) {
            return response()->json([
                'dataset' => view('hack::builder.ajax.index')
                    ->with('datas', $datas)
                    ->with('fid', $request->fid)
                    ->with('searchFields', $this->searchFields)
                    ->render(),
                'paginate' => view('hack::model.ajax.paginate')
                    ->with('datas', $datas)
                    ->with('fid', $request->fid)
                    ->with('searchFields', $this->searchFields)
                    ->render(),
            ]);
        }

        return view('hack::builder.index')
            ->with('datas', $datas)
            ->with('fid', $request->fid);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function module(Request $request)
    {
        return view('hack::builder.module')
            ->with('fid', $request->fid);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        if(session('_old_input')) {
            $data = session('_old_input');
        }
        else {
            $data = [];
        }

        // get the type values
        $namespace = config('cms.builders.'.$request->module.'.namespace');

        $moduleClass = new $namespace;

        foreach($moduleClass->types as $key => $values) {
            if(array_key_exists('builder', $values) && ! $values['builder']) {
                unset($moduleClass->types[$key]);
            }
        }

        return view('hack::builder.create')
            ->with('data', $data)
            ->with('types', $moduleClass->types)
            ->with('fid', $request->fid)
            ->with('module', $request->module);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(BuilderStore $request)
    {

        // Start insert
        try {
            DB::beginTransaction();

            // get the type values
            $namespace = config('cms.builders.'.$request->module.'.namespace');

            $moduleClass = new $namespace;

            $moduleId = $moduleClass->insertGetId($request->except($this->except)+[
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ]);

            $templateableId = $this->templateable->insertGetId([
                'template_id' => $request->fid,
                'templateable_id' => $moduleId,
                'templateable_type' => $namespace,
            ]);

            // Add the newly created to the current Page
            $pageIds = $this->page
                ->where('template_id', $request->fid)
                ->pluck('id');

            foreach($pageIds as $pageId) {

                $pageModuleId = $moduleClass->insertGetId($request->except($this->except)+[
                    'template_sibling' => $moduleId,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ]);

                $pageableId = $this->pageable->insertGetId([
                    'page_id' => $pageId,
                    'pageable_id' => $pageModuleId,
                    'pageable_type' => $namespace,
                ]);
            }

            DB::commit();

            DbLog::add(__CLASS__, 'store', json_encode($request->all()));

            Cms::destroyCache([$this->slug]);
        }
        catch(Exception $e) {
            return $this->rollback($e, $request);
        }

        return redirect()->route('cms.builder.edit', ['id' => $templateableId, 'fid' => $request->fid])
            ->with('alert-success', trans('hack::cms.info.created'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request, $id)
    {
        $data = $this->templateable
            ->where('id', $id)
            ->first();

        $moduleClass = new $data->templateable_type;

        if(session('_old_input')) {
            $moduleData = session('_old_input');
        }
        else {
            $moduleData = $moduleClass
                ->where('id', $data->templateable_id)
                ->first()
                ->toArray();
        }

        foreach($moduleClass->types as $key => $values) {
            if(array_key_exists('builder', $values) && ! $values['builder']) {
                unset($moduleClass->types[$key]);
            }
        }

        return view('hack::builder.edit')
            ->with('types', $moduleClass->types)
            ->with('fid', $request->fid)
            ->with('data', $moduleData)
            ->with('id', $id);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(BuilderUpdate $request, $id)
    {
        // Start insert
        try {
            DB::beginTransaction();

            $data = $this->templateable
                ->where('id', $id)
                ->first();

            $moduleClass = new $data->templateable_type;

            array_push($this->except, 'module_id');

            $moduleClass->where('id', $request->module_id)
                ->update($request->except($this->except)+[
                    'updated_at' => date('Y-m-d H:i:s'),
                ]);

            // update all the sibling that derive from this module
            $moduleClass->where('template_sibling', $request->module_id)
                ->update($request->except($this->except+['value'])+[
                    'updated_at' => date('Y-m-d H:i:s'),
                ]);

            DB::commit();

            DbLog::add(__CLASS__, 'update', json_encode($request->all()));

            Cms::destroyCache([$this->slug]);
        }
        catch(Exception $e) {
            return $this->rollback($e, $request, 'update');
        }

        return redirect()->route('cms.'.$this->slug.'.edit', ['id' => $id, 'fid' => $request->fid])
            ->with('alert-success', trans('hack::cms.info.updated'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try {

            DB::beginTransaction();

            // get the pivot
            $pivot = $this->templateable->where('id', $id)->first();

            // get the class
            $moduleClass = new $pivot->templateable_type;

            // get the ids of the page items
            $pageIds = $moduleClass
                ->select('id')
                ->where('template_sibling', $pivot->templateable_id)
                ->pluck('id')
                ->toArray();

            // delete the pivots
            $this->pageable->whereIn('pageable_id', $pageIds)->delete();

            // delete the resource and its children
            $builder = $moduleClass
                ->where('template_sibling', $pivot->templateable_id)
                ->orWhere('id', $pivot->templateable_id)
                ->delete();

            // delete the pivots
            $this->templateable->where('id', $id)->delete();

            DB::commit();

            DbLog::add(__CLASS__, 'destroy', $id);

            Cms::destroyCache([$this->slug]);

            return response()->json([
                'message' => trans('hack::cms.deleted'),
            ]);

        }
        catch(Exception $e) {

            DB::rollBack();

            Log::error('Rollback after deletion attempt', [
                'action' => 'destroy',
                'data' => $id,
                'error' => $e->getMessage(),
            ]);

            return response()->json([
                'error' => '',
            ]);
        }

    }

    /**
     * ORDER
     *
     * Re-order the items
     */
    public function order(Request $request)
    {
        try {
            DB::beginTransaction();

            foreach($request->order as $order => $id) {

                $this->templateable->timestamps = false;
                $this->templateable->where('id', $id)->update([
                    'drag_order' => $order,
                ]);

                // get the record so we can update the siblings
                $templateable = $this->templateable->where('id', $id)->first();

                // get the namespace
                $namespace = $templateable->templateable_type;

                // get the builder
                $builderInstance = new $namespace;

                // get the sibling builders, aka page builders
                $buildersIds = $builderInstance->where('template_sibling', $templateable->templateable_id)->pluck('id')->toArray();

                // update pageables
                $this->pageable->timestamps = false;
                $this->pageable->whereIn('pageable_id', $buildersIds)
                    ->where('pageable_type', $namespace)
                    ->update([
                        'drag_order' => $order,
                    ]);
            }

            DbLog::add(__CLASS__, 'order', json_encode($request->all()));

            DB::commit();

            return response()->json([
                'success' => true,
            ]);
        }
        catch(Exception $e) {

            Log::error('Rollback after order attempt', [
                'action' => 'order',
                'data' => $request->order,
                'error' => $e->getMessage(),
            ]);

            if(env('APP_DEBUG') === true) {
                return response()->json([
                    'error' => $e->getMessage(),
                ], 500);
            }

            return response()->json([
                'error' => 'There was an error',
            ], 500);
        }
    }

}


text:
"""Contains the logic for `aq reset advertised status --hostname`."""

from aquilon.exceptions_ import ArgumentError, IncompleteError
from aquilon.worker.broker import BrokerCommand  # pylint: disable=W0611
from aquilon.worker.dbwrappers.host import hostname_to_host
from aquilon.worker.templates.domain import TemplateDomain
from aquilon.worker.templates.host import PlenaryHost
from aquilon.worker.locks import lock_queue, CompileKey

class CommandResetAdvertisedStatus(BrokerCommand):
    """ reset advertised status for single host """

    required_parameters = ["hostname"]

    def render(self, session, logger, hostname, **arguments):
        dbhost = hostname_to_host(session, hostname)
        if dbhost.status.name == 'ready':
            raise ArgumentError("{0:l} is in ready status, "
                               "advertised status can be reset only "
                               "when host is in non ready state".format(dbhost))

        dbhost.advertise_status = False

        session.add(dbhost)
        session.flush()

        if dbhost.archetype.is_compileable:
            return self.compile(session, logger, dbhost)

        return

    def compile(self, session, logger, dbhost):
        """ compile plenary templates """
        plenary = PlenaryHost(dbhost, logger=logger)
        # Force a host lock as pan might overwrite the profile...
        key = CompileKey(domain=dbhost.branch.name, profile=dbhost.fqdn,
                         logger=logger)
        try:
            lock_queue.acquire(key)
            plenary.write(locked=True)
            td = TemplateDomain(dbhost.branch, dbhost.sandbox_author,
                                logger=logger)
            td.compile(session, only=[dbhost.fqdn], locked=True)
        except IncompleteError:
            raise ArgumentError("Run aq make for host %s first." % dbhost.fqdn)
        except:
            plenary.restore_stash()
            raise
        finally:
            lock_queue.release(key)

redpajama_set_name: RedPajamaGithub


text:
var buster = require('buster-node'),
  formatter = require('../../lib/formatters/sshconfig'),
  referee = require('referee'),
  assert = referee.assert;

buster.testCase('sshconfig - format', {
  setUp: function () {
    this.mock({});
  },
  'should return empty string when conf does not have any host': function () {
    var conf = [];
    assert.equals(formatter.format(conf), '');
  },
  'should format config paragraph when conf only has a single host': function () {
    var conf = [
      { host: 'dev1.com', ssh: [{ user: 'user1', port: 2222, key: 'path/to/key1' }], labels: [ 'dev1' ] }
    ];
    assert.equals(formatter.format(conf), 'Host dev1\n    HostName dev1.com\n    IdentityFile path/to/key1\n    User user1\n    Port 2222\n\n');
  },
  'should not include host which label is used by multiple hosts': function () {
    var conf = [
      { host: 'dev1.com', ssh: [{ user: 'user1', key: 'path/to/key1' }], labels: [ 'dev1' ] },
      { host: 'dev2.com', ssh: [{ user: 'user2', key: 'path/to/key2' }], labels: [ 'shared' ] },
      { host: 'dev3.com', ssh: [{ user: 'user3', key: 'path/to/key3' }], labels: [ 'shared' ] }
    ];
    assert.equals(formatter.format(conf), 'Host dev1\n    HostName dev1.com\n    IdentityFile path/to/key1\n    User user1\n\n');
  },
  'should sort by label when there are multiple config paragraphs': function () {
    var conf = [
      { host: 'dev1.com', ssh: [{ user: 'user1' }], labels: [ 'ccc' ] },
      { host: 'dev2.com', ssh: [{ user: 'user2' }], labels: [ 'aaa' ] },
      { host: 'dev3.com', ssh: [{ user: 'user3' }], labels: [ 'bbb' ] }
    ];
    assert.equals(formatter.format(conf),
      'Host aaa\n    HostName dev2.com\n    User user2\n\n\n' +
      'Host bbb\n    HostName dev3.com\n    User user3\n\n\n' +
      'Host ccc\n    HostName dev1.com\n    User user1\n\n'
    );
  },
  'should format config without key, user, and port': function () {
    var conf = [
      { host: 'dev1.com', ssh: [{}], labels: [ 'dev1' ] }
    ];
    assert.equals(formatter.format(conf), 'Host dev1\n    HostName dev1.com\n\n');
  },
  'should format config without ssh as empty string': function () {
    var conf = [
      { host: 'dev1.com', labels: [ 'dev1' ] }
    ];
    assert.equals(formatter.format(conf), '');
  }
});


text:
package smf

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"

	"gitlab.com/gomidi/midi/v2"
	"gitlab.com/gomidi/midi/v2/internal/runningstatus"
	"gitlab.com/gomidi/midi/v2/internal/utils"
)

type Logger interface {
	Printf(format string, vals ...interface{})
}

// ReadFile opens file, creates the SMF and closes file
func ReadFile(file string, opts ...ReadOption) (*SMF, error) {
	f, err := os.Open(file)

	if err != nil {
		return nil, err
	}

	defer func() {
		f.Close()
	}()

	return ReadFrom(f, opts...)
}

// ReadOption is an option for reading of SMF files
type ReadOption func(*readConfig)

func Log(l Logger) ReadOption {
	return func(c *readConfig) {
		c.Logger = l
	}
}

type readConfig struct {
	Logger Logger
}

// ReadFrom reads a SMF from the given io.Reader
func ReadFrom(f io.Reader, opts ...ReadOption) (*SMF, error) {

	var c readConfig

	for _, opt := range opts {
		opt(&c)
	}

	rd := newReader(f)
	rd.Logger = c.Logger

	err := rd.ReadHeader()

	if err != nil {
		return nil, err
	}

	//fmt.Printf("SMF: %#v\n", *rd.SMF)

	err = rd.ReadTracks()
	if rd.tracksMissing() {
		return nil, ErrMissing
	}

	if err == ErrFinished || err == io.EOF {
		return rd.SMF, nil
	}

	if err != nil {
		return nil, err
	}

	return rd.SMF, nil
}

// newReader returns a smf.Reader
func newReader(src io.Reader) *reader {
	rd := &reader{
		input:           src,
		processedTracks: -1,
		runningStatus:   runningstatus.NewSMFReader(),
		SMF:             &SMF{},
	}

	return rd
}

// Close closes the internal reader if it is an io.ReadCloser
func (r *reader) Close() error {
	if cl, is := r.input.(io.ReadCloser); is {
		return cl.Close()
	}
	return nil
}

func (r *reader) ReadHeader() error {
	if r.input == nil {
		return fmt.Errorf("no input defined")
	}
	if r.headerIsRead {
		return r.error
	}
	r.error = r.readMThd()
	r.headerIsRead = true

	if r.error != nil {
		return r.error
	}

	for i := 0; i < int(r.numTracks); i++ {
		r.Tracks = append(r.Tracks, Track{})
	}

	return r.error
}

type reader struct {
	*SMF
	Logger Logger

	input               io.Reader
	isDone              bool
	expectChunk         bool
	expectedChunkLength uint32
	runningStatus       runningstatus.Reader
	processedTracks     int16
	deltatime           uint32
	headerIsRead        bool
	error               error
}

// Delta returns the delta time in ticks for the last MIDI message
func (r *reader) Delta() uint32 {
	return r.deltatime
}

// Track returns the track for the last MIDI message
func (r *reader) Track() int16 {
	return r.processedTracks
}

func (r *reader) tracksMissing() bool {
	// allow the last track to skip the endoftrack message
	//return r.processedTracks+1 < int16(r.numTracks)
	return int(r.numTracks) > int(r.processedTracks)+1
}

func (r *reader) ReadTracks() (err error) {
	var m Message
	var absTicks int64

	for {
		m, err = r.Read()
		if err != nil {
			break
		}

		r.log("message %v", m)
		//fmt.Printf("message %v\n", m)
		tr := int(r.Track())

		/*
			// TODO maybe remove this after lots of tests
			if m == nil {
				continue
			}
		*/

		if m.Is(MetaEndOfTrackMsg) {
			r.log("end of track")
			r.Tracks[tr].Close(r.deltatime)
			absTicks = 0
			continue
		}

		absTicks += int64(r.deltatime)

		if m.Is(MetaTempoMsg) {
			tc := TempoChange{
				AbsTicks: absTicks,
			}

			m.GetMetaTempo(&tc.BPM)
			//fmt.Printf("BPM: %v\n", tc.BPM)
			r.SMF.tempoChanges = append(r.SMF.tempoChanges, &tc)
		}

		r.log("add message %v to track %v", m, tr)
		//fmt.Printf("add message %v to track %v\n", m, tr)
		r.Tracks[tr].Add(r.deltatime, m)
	}

	r.SMF.finishTempoChanges()

	return err
}

// Read reads the next midi message
// If the file has been read completely, ErrFinished is returned as error.
func (r *reader) Read() (m Message, err error) {
	msg, err := r.read()
	if err == io.EOF && r.tracksMissing() {
		return m, ErrMissing
	}
	return msg, err
}

func (r *reader) read() (m Message, err error) {
	if r.isDone {
		return m, ErrFinished
	}

	if !r.headerIsRead {
		r.error = r.ReadHeader()
	}

	if r.error != nil {
		return m, r.error
	}

	//fmt.Println("expectChunk", r.expectChunk)

	if r.expectChunk {
		r.readChunk()
	}

	if r.error != nil {
		return m, r.error
	}

	// now we are inside a track
	r.deltatime = 0
	m, r.error = r.readEvent()
	return m, r.error
}

func (r *reader) log(format string, vals ...interface{}) {
	if r.Logger != nil {
		r.Logger.Printf(format+"\n", vals...)
	}
}

func (r *reader) readMThd() (err error) {

	// after the header a chunk should come
	r.expectChunk = true

	var chunk chunk

	_, err = chunk.ReadHeader(r.input)
	r.log("reading header of chunk, error: %v", err)

	if err != nil {
		return
	}

	if chunk.Type() != "MThd" {
		r.log("wrong chunker type: %v", chunk.Type())
		err = errExpectedMthd
		return
	}

	err = r.parseHeaderData(r.input)
	r.log("reading body of header type: %v", err)

	return // leave at the end
}

func (r *reader) readChunk() {

	if r.error != nil {
		return
	}

	var chunk chunk

	r.expectedChunkLength, r.error = chunk.ReadHeader(r.input)
	r.log("reading header of chunk: %v", r.error)

	if r.error != nil {
		// if we are here, not all tracks have been read, so io.EOF would be an error,
		// so return errors here in each case
		return
	}

	r.log("got chunk type: %v", chunk.Type())
	// We have a MTrk
	if chunk.Type() == "MTrk" {
		r.log("is track chunk")
		r.processedTracks++
		r.expectChunk = false
		// we are done, lets go to the track events
		return
	}

	// The header is of an unknown type, skip over it.
	_, r.error = io.CopyN(ioutil.Discard, r.input, int64(r.expectedChunkLength))
	r.log("skipping chunk: %v", r.error)
	if r.error != nil {
		return
	}

	r.expectChunk = true
}

func (r *reader) _readEvent(canary byte) (m Message, err error) {
	r.log("_readEvent, canary: % X", canary)
	//	msgType := midi.UndefinedMsgType

	status, changed := r.runningStatus.Read(canary)
	r.log("got status: % X, changed: %v", status, changed)

	var isMetaEndOfTrackMsg bool

	// a non-channel message has reset the status
	if status == 0 {

		switch canary {

		// both 0xF0 and 0xF7 may start a sysex in SMF files
		case 0xF0, 0xF7:
			r.log("found sysex")
			var ln uint32
			ln, err = utils.ReadVarLength(r.input)
			if err != nil {
				return m, err
			}
			bt, err := utils.ReadNBytes(int(ln), r.input)
			if err != nil {
				return m, err
			}
			//return midi.SysEx(bt).Bytes(), nil
			return Message(append([]byte{canary}, bt...)), nil

		// meta event
		case 0xFF:
			var typ byte
			typ, err = utils.ReadByte(r.input)
			r.log("read meta message type: % X, err: %v", typ, err)

			if err != nil {
				return m, err
			}

			var ln uint32
			ln, err = utils.ReadVarLength(r.input)
			if err != nil {
				return m, err
			}
			var bt []byte
			bt, err = utils.ReadNBytes(int(ln), r.input)
			if err != nil {
				return m, err
			}
			//m.Data = bt
			mm := _MetaMessage(typ, bt)

			if mm.Is(MetaEndOfTrackMsg) {
				isMetaEndOfTrackMsg = true
			}

			// since System Common messages are not allowed within smf files, there could only be meta messages
			// all (event unknown) meta messages must be handled by the meta dispatcher
			//m, err = newMetaReader(r.input, typ).Read()
			//r.log("got meta: %T data: % X", m.MsgType, m.Data)
			r.log("got meta: %s data: % X\n", mm.Type(), mm)
			//fmt.Printf("got meta: %s\n", mm)
			m = mm

		default:
			panic(fmt.Sprintf("must not happen: invalid canary % X", canary))
		}

		// on a voice/channel category message with status either given or cached (running status)
	} else {
		var arg1 = canary // assume running status - we already got arg1

		// was no running status, we have to read arg1
		if changed {
			arg1, err = utils.ReadByte(r.input)
			if err != nil {
				return
			}
		}

		var mim midi.Message
		mim, err = midi.ReadChannelMessage(status, arg1, r.input)
		m = mim.Bytes()

		// since every possible status is covered by a voice message type, m can't be nil
		r.log("got channel message: %#v, err: %v", m, err)
	}

	if err != nil {
		r.log("got err: %v", err)
	}

	if isMetaEndOfTrackMsg {
		r.log("got end of track")

		// Expect the next chunk midi.

		// TODO check the read length of the track against the length thas has been read
		// return ErrTruncatedTrack if meta.EndOfTrack comes to early or ErrOverflowingTrack it it comes too late
		if uint16(r.processedTracks+1) == r.numTracks {
			r.log("last track has been read")
			r.isDone = true
		} else {
			r.expectChunk = true
		}
	}

	r.log("returning: %v", m)
	return m, nil
}

func (r *reader) readEvent() (m Message, err error) {
	if r.error != nil {
		return m, r.error
	}

	//fmt.Println("readevent called")

	var deltatime uint32

	deltatime, err = utils.ReadVarLength(r.input)
	r.log("read delta: %v, err: %v", deltatime, err)
	if err != nil {
		return
	}

	r.deltatime = deltatime

	// read the canary in the coal mine to see, if we have a running status byte or a given one
	var canary byte
	canary, err = utils.ReadByte(r.input)
	r.log("read canary: %v, err: %v", canary, err)

	//fmt.Printf("read canary: %v, err: %v", canary, err)

	if err != nil {
		return
	}

	return r._readEvent(canary)
}

// parseHeaderData parses SMF-header chunk header data.
func (r *reader) parseHeaderData(reader io.Reader) error {

	format, err := utils.ReadUint16(reader)

	if err != nil {
		return err
	}

	switch format {
	case 0:
		r.format = 0
	case 1:
		r.format = 1
	case 2:
		r.format = 2
	default:
		return errUnsupportedSMFFormat
	}

	r.numTracks, err = utils.ReadUint16(reader)

	if err != nil {
		return err
	}

	var division uint16
	division, err = utils.ReadUint16(reader)

	if err != nil {
		return err
	}

	// "If bit 15 of <division> is zero, the bits 14 thru 0 represent the number
	// of delta time "ticks" which make up a quarter-note."
	if division&0x8000 == 0x0000 {
		r.TimeFormat = MetricTicks(division & 0x7FFF)
	} else {
		r.TimeFormat = parseTimeCode(division)
	}

	/*
			The last two bytes indicate how many Pulses (i.e. clocks) Per Quarter Note
			(abbreviated as PPQN) resolution the time-stamps are based upon, Division.
			For example, if your sequencer has 96 ppqn, this field would be (in hex):

		00 60

		Alternately, if the first byte of Division is negative, then this represents
		the division of a second that the time-stamps are based upon. The first byte
		will be -24, -25, -29, or -30, corresponding to the 4 SMPTE standards
		representing frames per second. The second byte (a positive number)
		is the resolution within a frame (ie, subframe). Typical values may
		be 4 (MIDI Time Code), 8, 10, 80 (SMPTE bit resolution), or 100.

		You can specify millisecond-based timing by the data bytes of -25 and 40 subframes.
	*/

	/* http://www.somascape.org/midi/tech/mfile.html

	   tickdiv : specifies the timing interval to be used, and whether timecode (Hrs.Mins.Secs.Frames) or metrical (Bar.Beat) timing is to be used. With metrical timing, the timing interval is tempo related, whereas with timecode the timing interval is in absolute time, and hence not related to tempo.

	       Bit 15 (the top bit of the first byte) is a flag indicating the timing scheme in use :

	       Bit 15 = 0 : metrical timing
	       Bits 0 - 14 are a 15-bit number indicating the number of sub-divisions of a quarter note (aka pulses per quarter note, ppqn). A common value is 96, which would be represented in hex as 00 60. You will notice that 96 is a nice number for dividing by 2 or 3 (with further repeated halving), so using this value for tickdiv allows triplets and dotted notes right down to hemi-demi-semiquavers to be represented.

	       Bit 15 = 1 : timecode
	       Bits 8 - 15 (i.e. the first byte) specifies the number of frames per second (fps),
	       and will be one of the four SMPTE standards - 24, 25, 29 or 30, though expressed as a negative value
	       (using 2's complement notation), as follows :
	       fps	Representation (hex)
	       24 E8
	       25 E7
	       29 E3
	       30 E2

	       Bits 0 - 7 (the second byte) specifies the sub-frame resolution, i.e. the number of sub-divisions of a frame.
	       Typical values are 4 (corresponding to MIDI Time Code), 8, 10, 80 (corresponding to SMPTE bit resolution), or 100.

	       A timing resolution of 1 ms can be achieved by specifying 25 fps and 40 sub-frames, which would be encoded in hex as  E7 28.

	   A complete MThd chunk thus contains 14 bytes (including the 8 byte header).
	   Example
	   Data (hex)	Interpretation
	   4D 54 68 64 	identifier, the ascii chars 'MThd'
	   00 00 00 06 	chunklen, 6 bytes of data follow . . .
	   00 01 	format = 1
	   00 11 	ntracks = 17
	   00 60 	tickdiv = 96 ppqn, metrical time

	*/

	return nil
}

// Parse parses the timecode from the raw value returned from Header.TimeFormat if the format is TimeCode
// It returns SMPTE frames per second (29 corresponds to 30 drop frame) and the subframes.
func parseTimeCode(raw uint16) (t TimeCode) {
	// bit shifting first byte to second inverting sign
	t.FramesPerSecond = uint8(int8(byte(raw>>8)) * (-1))

	// taking the second byte
	t.SubFrames = byte(raw & uint16(255))
	return
}


text:
puts 'What is your first name?'
first_name = gets.chomp
puts 'What is your middle name?'
middle_name = gets.chomp
puts 'What is your last name?'
last_name = gets.chomp
puts 'Hello ' + first_name + ' ' + middle_name + ' ' + last_name + '!'

puts 'What is your favorite number?'
fav_number = gets.chomp
better_number = fav_number.to_i+1
puts 'I think ' + better_number.to_s + ' is better!'

#Reflection questions for 4.3 Variables and Methods

#How do you define a local variable?
#Choose a name for the variable, it must start with a lowercase letter and use _ in between multiple words. Set it equal to the value you want it to have for later when you use the variable. For example: my_variable = "Katie"

#How do you define a method?
#Write "def" followed by the name of the variable with its parameters in parentheses. On the next line, write what you want the method to do when it runs. You need to include "end" at the end. Then to run the method, you need to call it. For example:
#def my_method
  #puts "Hello Katie!"
#end
#my_method

#What is the difference between a local variable and a method?
#Variables hold information that will be used later. Methods are things that do stuff to things like strings, integers, and floats.

#How do you run a ruby program from the command line?
#ruby file_name.rb

#How do you run an RSpec file from the command line?
#rspec file_name.rb

#What was confusing about this material? What made sense?
#The directions were confusing but once I got past that, I had a hard time passing some tests until I realized I needed to use "return" instead of "puts" as in the address exercise.

#Links to the exercises:

#Return a Formatted Address: https://github.com/kmeyer313/phase-0/blob/master/week-4/address/my_solution.rb

#Defining Math Methods: https://github.com/kmeyer313/phase-0/blob/master/week-4/math/my_solution.rb


text:
#pragma once

#ifndef GEODE_CONNECTCOUNTER_H_
#define GEODE_CONNECTCOUNTER_H_

#include <geode/internal/geode_globals.hpp>

namespace apache {
namespace geode {
namespace client {

class APACHE_GEODE_EXPORT ConnectCounter {
 public:
  ConnectCounter();

  virtual ~ConnectCounter();

  virtual void inc(const char* clientName) = 0;
  virtual void dec(const char* clientName) = 0;
};
}  // namespace client
}  // namespace geode
}  // namespace apache

#endif  // GEODE_CONNECTCOUNTER_H_


text:
// LookingGlassOgre.cpp : Defines the exported functions for the DLL application.

// #include "stdafx.h"
#include "LGLocking.h"

namespace LG {

int LGLockingThreadInitializeCount = 0;

LGLock::LGLock() { }
LGLock::LGLock(Ogre::String nam) {
	Name = nam;
#if defined(LGLOCK_BOOST)
	m_mutex = new boost::mutex();
	m_condition = new boost::condition();
#endif
}
LGLock::~LGLock() {
#if defined(LGLOCK_BOOST)
	delete m_mutex;
	delete m_condition;
	m_mutex = NULL;
#endif
}

// =========================================
void LGLock::Lock() {
#ifdef LGLOCK_BOOST
	m_mutex->lock();
#endif
	return;
}
// =========================================
void LGLock::Unlock() {
#ifdef LGLOCK_BOOST
	m_mutex->unlock();
#endif
	return;
}

LGLock* LGLock::LGLock_Allocate_Mutex(Ogre::String name) {
	return new LGLock(name);
}

void LGLock::LGLock_Release_Lock(LGLock* lock) {
	if (lock != NULL) delete lock;
}

#ifdef LGLOCK_BOOST
void LGLock::LGLock_Sleep(int ms) {
	boost::xtime xt;
	boost::xtime_get(&xt, boost::TIME_UTC);
	xt.sec += ms;

	boost::thread::sleep(xt);
}
#endif

}


text:
from __future__ import annotations

import os
from dataclasses import dataclass
from pathlib import Path

from airflow_breeze.branch_defaults import AIRFLOW_BRANCH, DEFAULT_AIRFLOW_CONSTRAINTS_BRANCH
from airflow_breeze.global_constants import (
    ALLOWED_BACKENDS,
    ALLOWED_CONSTRAINTS_MODES_CI,
    ALLOWED_INSTALLATION_PACKAGE_FORMATS,
    ALLOWED_MSSQL_VERSIONS,
    ALLOWED_MYSQL_VERSIONS,
    ALLOWED_POSTGRES_VERSIONS,
    ALLOWED_PYTHON_MAJOR_MINOR_VERSIONS,
    APACHE_AIRFLOW_GITHUB_REPOSITORY,
    AVAILABLE_INTEGRATIONS,
    DOCKER_DEFAULT_PLATFORM,
    MOUNT_ALL,
    MOUNT_REMOVE,
    MOUNT_SELECTED,
    MOUNT_SKIP,
    get_airflow_version,
)
from airflow_breeze.utils.console import get_console
from airflow_breeze.utils.path_utils import AIRFLOW_SOURCES_ROOT, BUILD_CACHE_DIR, SCRIPTS_CI_DIR
from airflow_breeze.utils.run_utils import get_filesystem_type, run_command

@dataclass
class ShellParams:
    """
    Shell parameters. Those parameters are used to determine command issued to run shell command.
    """

    airflow_branch: str = os.environ.get('DEFAULT_BRANCH', AIRFLOW_BRANCH)
    default_constraints_branch: str = os.environ.get(
        'DEFAULT_CONSTRAINTS_BRANCH', DEFAULT_AIRFLOW_CONSTRAINTS_BRANCH
    )
    airflow_constraints_reference: str = DEFAULT_AIRFLOW_CONSTRAINTS_BRANCH
    airflow_extras: str = ""
    answer: str | None = None
    backend: str = ALLOWED_BACKENDS[0]
    ci: bool = False
    db_reset: bool = False
    dev_mode: bool = False
    dry_run: bool = False
    extra_args: tuple = ()
    force_build: bool = False
    forward_credentials: str = "false"
    airflow_constraints_mode: str = ALLOWED_CONSTRAINTS_MODES_CI[0]
    github_actions: str = os.environ.get('GITHUB_ACTIONS', "false")
    github_repository: str = APACHE_AIRFLOW_GITHUB_REPOSITORY
    github_token: str = os.environ.get('GITHUB_TOKEN', "")
    image_tag: str | None = None
    include_mypy_volume: bool = False
    install_airflow_version: str = ""
    install_providers_from_sources: bool = True
    integration: tuple[str, ...] = ()
    issue_id: str = ""
    load_default_connections: bool = False
    load_example_dags: bool = False
    mount_sources: str = MOUNT_SELECTED
    mssql_version: str = ALLOWED_MSSQL_VERSIONS[0]
    mysql_version: str = ALLOWED_MYSQL_VERSIONS[0]
    num_runs: str = ""
    package_format: str = ALLOWED_INSTALLATION_PACKAGE_FORMATS[0]
    platform: str = DOCKER_DEFAULT_PLATFORM
    postgres_version: str = ALLOWED_POSTGRES_VERSIONS[0]
    python: str = ALLOWED_PYTHON_MAJOR_MINOR_VERSIONS[0]
    skip_environment_initialization: bool = False
    skip_constraints: bool = False
    start_airflow: str = "false"
    use_airflow_version: str | None = None
    use_packages_from_dist: bool = False
    verbose: bool = False
    version_suffix_for_pypi: str = ""

    @property
    def airflow_version(self):
        return get_airflow_version()

    @property
    def airflow_version_for_production_image(self):
        cmd = ['docker', 'run', '--entrypoint', '/bin/bash', f'{self.airflow_image_name}']
        cmd.extend(['-c', 'echo "${AIRFLOW_VERSION}"'])
        output = run_command(cmd, capture_output=True, text=True)
        return output.stdout.strip() if output.stdout else "UNKNOWN_VERSION"

    @property
    def airflow_base_image_name(self) -> str:
        image = f'ghcr.io/{self.github_repository.lower()}'
        return image

    @property
    def airflow_image_name(self) -> str:
        """Construct CI image link"""
        image = f'{self.airflow_base_image_name}/{self.airflow_branch}/ci/python{self.python}'
        return image

    @property
    def airflow_image_name_with_tag(self) -> str:
        image = self.airflow_image_name
        return image if not self.image_tag else image + f":{self.image_tag}"

    @property
    def airflow_image_kubernetes(self) -> str:
        image = f'{self.airflow_base_image_name}/{self.airflow_branch}/kubernetes/python{self.python}'
        return image

    @property
    def airflow_sources(self):
        return AIRFLOW_SOURCES_ROOT

    @property
    def enabled_integrations(self) -> str:
        if "all" in self.integration:
            enabled_integration = " ".join(AVAILABLE_INTEGRATIONS)
        elif len(self.integration) > 0:
            enabled_integration = " ".join(self.integration)
        else:
            enabled_integration = ""
        return enabled_integration

    @property
    def image_type(self) -> str:
        return 'CI'

    @property
    def md5sum_cache_dir(self) -> Path:
        cache_dir = Path(BUILD_CACHE_DIR, self.airflow_branch, self.python, self.image_type)
        return cache_dir

    @property
    def backend_version(self) -> str:
        version = ''
        if self.backend == 'postgres':
            version = self.postgres_version
        if self.backend == 'mysql':
            version = self.mysql_version
        if self.backend == 'mssql':
            version = self.mssql_version
        return version

    @property
    def sqlite_url(self) -> str:
        sqlite_url = "sqlite:////root/airflow/airflow.db"
        return sqlite_url

    def print_badge_info(self):
        if self.verbose:
            get_console().print(f'[info]Use {self.image_type} image[/]')
            get_console().print(f'[info]Branch Name: {self.airflow_branch}[/]')
            get_console().print(f'[info]Docker Image: {self.airflow_image_name_with_tag}[/]')
            get_console().print(f'[info]Airflow source version:{self.airflow_version}[/]')
            get_console().print(f'[info]Python Version: {self.python}[/]')
            get_console().print(f'[info]Backend: {self.backend} {self.backend_version}[/]')
            get_console().print(f'[info]Airflow used at runtime: {self.use_airflow_version}[/]')

    def get_backend_compose_files(self, backend: str):
        backend_docker_compose_file = f"{str(SCRIPTS_CI_DIR)}/docker-compose/backend-{backend}.yml"
        backend_port_docker_compose_file = f"{str(SCRIPTS_CI_DIR)}/docker-compose/backend-{backend}-port.yml"
        return backend_docker_compose_file, backend_port_docker_compose_file

    @property
    def compose_files(self):
        compose_ci_file = []
        main_ci_docker_compose_file = f"{str(SCRIPTS_CI_DIR)}/docker-compose/base.yml"
        if self.backend != "all":
            backend_files = self.get_backend_compose_files(self.backend)
        else:
            backend_files = []
            for backend in ALLOWED_BACKENDS:
                backend_files.extend(self.get_backend_compose_files(backend))
            compose_ci_file.append(f"{str(SCRIPTS_CI_DIR)}/docker-compose/backend-mssql-bind-volume.yml")
            compose_ci_file.append(f"{str(SCRIPTS_CI_DIR)}/docker-compose/backend-mssql-docker-volume.yml")
        local_docker_compose_file = f"{str(SCRIPTS_CI_DIR)}/docker-compose/local.yml"
        local_all_sources_docker_compose_file = f"{str(SCRIPTS_CI_DIR)}/docker-compose/local-all-sources.yml"
        files_docker_compose_file = f"{str(SCRIPTS_CI_DIR)}/docker-compose/files.yml"
        remove_sources_docker_compose_file = f"{str(SCRIPTS_CI_DIR)}/docker-compose/remove-sources.yml"
        mypy_docker_compose_file = f"{str(SCRIPTS_CI_DIR)}/docker-compose/mypy.yml"
        forward_credentials_docker_compose_file = (
            f"{str(SCRIPTS_CI_DIR)}/docker-compose/forward-credentials.yml"
        )
        # mssql based check have to be added
        if self.backend == 'mssql':
            docker_filesystem = get_filesystem_type('.')
            if docker_filesystem == 'tmpfs':
                compose_ci_file.append(f"{str(SCRIPTS_CI_DIR)}/docker-compose/backend-mssql-bind-volume.yml")
            else:
                compose_ci_file.append(
                    f"{str(SCRIPTS_CI_DIR)}/docker-compose/backend-mssql-docker-volume.yml"
                )
        compose_ci_file.extend([main_ci_docker_compose_file, *backend_files, files_docker_compose_file])

        if self.image_tag is not None and self.image_tag != "latest":
            get_console().print(
                f"[warning]Running tagged image tag = {self.image_tag}. "
                f"Forcing mounted sources to be 'skip'[/]"
            )
            self.mount_sources = MOUNT_SKIP
        if self.use_airflow_version is not None:
            get_console().print(
                "[info]Forcing --mount-sources to `remove` since we are not installing airflow "
                f"from sources but from {self.use_airflow_version}[/]"
            )
            self.mount_sources = MOUNT_REMOVE
        if self.mount_sources == MOUNT_SELECTED:
            compose_ci_file.extend([local_docker_compose_file])
        elif self.mount_sources == MOUNT_ALL:
            compose_ci_file.extend([local_all_sources_docker_compose_file])
        elif self.mount_sources == MOUNT_REMOVE:
            compose_ci_file.extend([remove_sources_docker_compose_file])
        if self.forward_credentials:
            compose_ci_file.append(forward_credentials_docker_compose_file)
        if self.use_airflow_version is not None:
            compose_ci_file.append(remove_sources_docker_compose_file)
        if self.include_mypy_volume:
            compose_ci_file.append(mypy_docker_compose_file)
        if "all" in self.integration:
            integrations = AVAILABLE_INTEGRATIONS
        else:
            integrations = self.integration
        if len(integrations) > 0:
            for integration in integrations:
                compose_ci_file.append(f"{str(SCRIPTS_CI_DIR)}/docker-compose/integration-{integration}.yml")
        return os.pathsep.join(compose_ci_file)

    @property
    def command_passed(self):
        cmd = None
        if len(self.extra_args) > 0:
            cmd = str(self.extra_args[0])
        return cmd


text:
/*global module:true*/
/*global require:true*/
/*global __dirname:true*/
(function(){
	"use strict";
	var path = require( 'path' );
	var phantomJsPath = require( 'phantomjs-prebuilt' ).path;
	var execFile = require( 'child_process' ).execFile;
	var phantomfile = path.join( __dirname, "phantomscript.js" );

	module.exports = function( contents, name, color, callback ){
			execFile( phantomJsPath,
				[
					phantomfile,
					encodeURI(contents),
					name,
					color
				],
				function(err, stdout){
					var svg = "";
					if( err ){
						callback( err, null );
						return;
					}

					svg = JSON.parse(decodeURI(stdout));

					callback( null, svg );
				}
			);
	};

}());


text:
title: "Legacy Planner"
weight: 1002
type: docs
aliases:
  - /dev/table/legacy_planner.html
---

# Legacy Planner

Table planners are responsible for translating relational operators into an executable, optimized Flink job.
Flink supports two different planner implementations; the modern planner (sometimes referred to as `Blink`) and the legacy planner.
For production use cases, we recommend the modern planner which is the default.

The legacy planner is in maintenance mode and no longer under active development.
The primary reason to continue using the legacy planner is [DataSet]({{< ref "docs/dev/dataset/overview" >}}) interop.

{{< hint warning >}}
If you are not using the Legacy planner for DataSet interop, the community strongly
encourages you to consider the modern table planner.
The legacy planner will be dropped at some point in the future.
{{< /hint >}}

This page describes how to use the Legacy planner and where its semantics differ from the
modern planner.

## Setup

### Dependencies

When deploying to a cluster, the legacy planner is bundled in Flinks distribution by default.
If you want to run the Table API & SQL programs locally within your IDE, you must add the
following set of modules to your application.

```xml
<dependency>
  <groupId>org.apache.flink</groupId>
  <artifactId>flink-table-planner{{< scala_version >}}</artifactId>
  <version>{{< version >}}</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.apache.flink</groupId>
  <artifactId>flink-streaming-scala{{< scala_version >}}</artifactId>
  <version>{{< version >}}</version>
  <scope>provided</scope>
</dependency>
```

### Configuring the TableEnvironment

When creating a `TableEnvironment` the Legacy planner is configured via the `EnvironmentSettings`.

{{< tabs "95cbb391-0326-4830-b38e-a5871c87d19f" >}}
{{< tab "Java" >}}
```java
EnvironmentSettings settings = EnvironmentSettings
    .newInstance()
    .useOldPlanner()
    .inStreamingMode()
    // or in batch mode
    //.inBatchMode()
    .build();

TableEnvironment tEnv = TableEnvironment.create(settings);
```
{{< /tab >}}
{{< tab "Scala" >}}
```scala
val settings = EnvironmentSettings
    .newInstance()
    .useOldPlanner()
    .inStreamingMode()
    // or in batch mode
    //.inBatchMode()
    .build()

val tEnv = TableEnvironment.create(settings)
```
{{< /tab >}}
{{< /tabs >}}

`BatchTableEnvironment` may used for [DataSet]({{< ref "docs/dev/dataset/overview" >}}) and [DataStream]({{< ref "docs/dev/datastream/overview" >}}) interop respectively.

{{< tabs "3d8abf94-f59c-4756-a5c0-7b645bf43870" >}}
{{< tab "Java" >}}
```java
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
BatchTableEnvironment tEnv = BatchTableEnvironment.create(env);
```
{{< /tab >}}
{{< tab "Scala" >}}
```scala
val env = ExecutionEnvironment.getExecutionEnvironment()
val tEnv = BatchTableEnvironment.create(env)
```
{{< /tab >}}
{{< tab "Python" >}}
```python
from pyflink.dataset import ExecutionEnvironment
from pyflink.table import BatchTableEnvironment

b_env = ExecutionEnvironment.get_execution_environment()
t_env = BatchTableEnvironment.create(b_env, table_config)
```
{{< /tab >}}
{{< /tabs >}}

## Integration with DataSet

The primary use case for the Legacy planner is interoperation with the DataSet API.
To translate `DataSet`s to and from tables, applications must use the `BatchTableEnvironment`.

### Create a View from a DataSet

A `DataSet` can be registered in a `BatchTableEnvironment` as a `View`.
The schema of the resulting view depends on the data type of the registered collection.

**Note:** Views created from a `DataSet` can be registered as temporary views only.

{{< tabs "02002c94-df73-4f10-b7b7-6c0f6fbf5909" >}}
{{< tab "Java" >}}
```java
BatchTableEnvironment tEnv = ...;
DataSet<Tuple2<Long, String>> dataset = ...;

tEnv.createTemporaryView("my-table", dataset, $("myLong"), $("myString"))
```
{{< /tab >}}
{{< tab "Scala" >}}
```java
val tEnv: BatchTableEnvironment = ???
val dataset: DataSet[(Long, String)] = ???

tEnv.createTemporaryView("my-table", dataset, $"myLong", $"myString")
```
{{< /tab >}}
{{< /tabs >}}

### Create a Table from a DataSet

A `DataSet` can be directly converted to a `Table` in a `BatchTableEnvironment`.
The schema of the resulting view depends on the data type of the registered collection.

{{< tabs "9bc01f1c-f24c-4be7-9c65-7f6575a12740" >}}
{{< tab "Java" >}}
```java
BatchTableEnvironment tEnv = ...;
DataSet<Tuple2<Long, String>> dataset = ...;

Table myTable = tEnv.fromDataSet("my-table", dataset, $("myLong"), $("myString"))
```
{{< /tab >}}
{{< tab "Scala" >}}
```java
val tEnv: BatchTableEnvironment = ???
val dataset: DataSet[(Long, String)] = ???

val table = tEnv.fromDataSet("my-table", dataset, $"myLong", $"myString")
```
{{< /tab >}}
{{< /tabs >}}

### Convert a Table to a DataSet

A `Table` can be converted to a `DataSet`.
In this way, custom DataSet programs can be run on the result of a Table API or SQL query.

When converting from a `Table`, users must specify the data type of the results.
Often the most convenient conversion type is `Row`.
The following list gives an overview of the features of the different options.

- **Row**: fields are mapped by position, arbitrary number of fields, support for `null` values, no type-safe access.
- **POJO**: fields are mapped by name (POJO fields must be named as `Table` fields), arbitrary number of fields, support for `null` values, type-safe access.
- **Case Class**: fields are mapped by position, no support for `null` values, type-safe access.
- **Tuple**: fields are mapped by position, limitation to 22 (Scala) or 25 (Java) fields, no support for `null` values, type-safe access.
- **Atomic Type**: `Table` must have a single field, no support for `null` values, type-safe access.

{{< tabs "c34bd733-f34e-4780-8cf0-c440465c9f58" >}}
{{< tab "Java" >}}
```java
BatchTableEnvironment tableEnv = BatchTableEnvironment.create(env);

Table table = tableEnv.fromValues(
    DataTypes.Row(
        DataTypes.FIELD("name", DataTypes.STRING()),
        DataTypes.FIELD("age", DataTypes.INT()),
    row("john", 35),
    row("sarah", 32));

// Convert the Table into a DataSet of Row by specifying a class
DataSet<Row> dsRow = tableEnv.toDataSet(table, Row.class);

// Convert the Table into a DataSet of Tuple2<String, Integer> via a TypeInformation
TupleTypeInfo<Tuple2<String, Integer>> tupleType = new TupleTypeInfo<>(Types.STRING(), Types.INT());
DataSet<Tuple2<String, Integer>> dsTuple = tableEnv.toDataSet(table, tupleType);
```
{{< /tab >}}
{{< tab "Scala" >}}
```scala
val tableEnv = BatchTableEnvironment.create(env)

val table = tableEnv.fromValues(
    DataTypes.Row(
        DataTypes.FIELD("name", DataTypes.STRING()),
        DataTypes.FIELD("age", DataTypes.INT()),
    row("john", 35),
    row("sarah", 32));

// Convert the Table into a DataSet of Row
val dsRow: DataSet[Row] = tableEnv.toDataSet[Row](table)

// Convert the Table into a DataSet of Tuple2[String, Int]
val dsTuple: DataSet[(String, Int)] = tableEnv.toDataSet[(String, Int)](table)
```
{{< /tab >}}
{{< /tabs >}}

Attention **Once the Table is converted to a DataSet, we must use the ExecutionEnvironment.execute method to execute the DataSet program.**

## Data Types

The legacy planner, introduced before Flink 1.9, primarily supports type information.
It has only limited support for data types.
It is possible to declare data types that can be translated into type information such that the legacy planner understands them.

The following table summarizes the difference between data type and type information.
Most simple types, as well as the row type remain the same.
Time types, array types, and the decimal type need special attention.
Other hints as the ones mentioned are not allowed.

For the *Type Information* column the table omits the prefix `org.apache.flink.table.api.Types`.

For the *Data Type Representation* column the table omits the prefix `org.apache.flink.table.api.DataTypes`.

| Type Information | Java Expression String | Data Type Representation | Remarks for Data Type |
|:-----------------|:-----------------------|:-------------------------|:----------------------|
| `STRING()` | `STRING` | `STRING()` | |
| `BOOLEAN()` | `BOOLEAN` | `BOOLEAN()` | |
| `BYTE()` | `BYTE` | `TINYINT()` | |
| `SHORT()` | `SHORT` | `SMALLINT()` | |
| `INT()` | `INT` | `INT()` | |
| `LONG()` | `LONG` | `BIGINT()` | |
| `FLOAT()` | `FLOAT` | `FLOAT()` | |
| `DOUBLE()` | `DOUBLE` | `DOUBLE()` | |
| `ROW(...)` | `ROW<...>` | `ROW(...)` | |
| `BIG_DEC()` | `DECIMAL` | [`DECIMAL()`] | Not a 1:1 mapping as precision and scale are ignored and Java's variable precision and scale are used. |
| `SQL_DATE()` | `SQL_DATE` | `DATE()`
`.bridgedTo(java.sql.Date.class)` | |
| `SQL_TIME()` | `SQL_TIME` | `TIME(0)`
`.bridgedTo(java.sql.Time.class)` | |
| `SQL_TIMESTAMP()` | `SQL_TIMESTAMP` | `TIMESTAMP(3)`
`.bridgedTo(java.sql.Timestamp.class)` | |
| `INTERVAL_MONTHS()` | `INTERVAL_MONTHS` | `INTERVAL(MONTH())`
`.bridgedTo(Integer.class)` | |
| `INTERVAL_MILLIS()` | `INTERVAL_MILLIS` | `INTERVAL(DataTypes.SECOND(3))`
`.bridgedTo(Long.class)` | |
| `PRIMITIVE_ARRAY(...)` | `PRIMITIVE_ARRAY<...>` | `ARRAY(DATATYPE.notNull()`
`.bridgedTo(PRIMITIVE.class))` | Applies to all JVM primitive types except for `byte`. |
| `PRIMITIVE_ARRAY(BYTE())` | `PRIMITIVE_ARRAY<BYTE>` | `BYTES()` | |
| `OBJECT_ARRAY(...)` | `OBJECT_ARRAY<...>` | `ARRAY(`
`DATATYPE.bridgedTo(OBJECT.class))` | |
| `MULTISET(...)` | | `MULTISET(...)` | |
| `MAP(..., ...)` | `MAP<...,...>` | `MAP(...)` | |
| other generic types | | `RAW(...)` | |

Attention If there is a problem with the new type system. Users
can fallback to type information defined in `org.apache.flink.table.api.Types` at any time.

## Unsupported Features

The following features are not supported by the legacy planner.

- [Deduplication]({{< ref "docs/dev/table/sql/queries" >}}#deduplication %})
- [Key Value Configurations]({{< ref "docs/dev/table/config" >}}#overview)
- [Streaming Aggregation Optimization]({{< ref "docs/dev/table/tuning" >}})
- Streaming mode Grouping sets, Rollup and Cube aggregations
- [Top-N]({{< ref "docs/dev/table/sql/queries" >}}#top-n)
- [Versioned Tables]({{< ref "docs/dev/table/concepts/versioned_tables" >}})

## Unsupported Built-In Functions

The following built-in functions are not supported by the legacy planner.

- `PI`
- `ASCII(string)`
- `CHR(integer)`
- `DECODE(binary, string)`
- `ENCODE(string1, string2)`
- `INSTR(string1, string2)`
- `LEFT(string, integer)`
- `RIGHT(string, integer)`
- `LOCATE(string1, string2[, integer])`
- `PARSE_URL(string1, string2[, string3])`
- `REGEXP(string1, string2)`
- `REVERSE(string)`
- `SPLIT_INDEX(string1, string2, integer1)`
- `STR_TO_MAP(string1[, string2, string3]])`
- `SUBSTR(string[, integer1[, integer2]])`
- `CONVERT_TZ(string1, string2, string3)`
- `FROM_UNIXTIME(numeric[, string])`
- `UNIX_TIMESTAMP()`
- `UNIX_TIMESTAMP(string1[, string2])`
- `TO_DATE(string1[, string2])`
- `TO_TIMESTAMP(string1[, string2])`
- `NOW()`
- `IF(condition, true_value, false_value)`
- `IS_ALPHA(string)`
- `IS_DECIMAL(string)`
- `IS_DIGIT(string)`
- `VARIANCE([ ALL | DISTINCT ] expression)`
- `RANK()`
- `DENSE_RANK()`
- `ROW_NUMBER()`
- `LEAD(expression [, offset] [, default] )`
- `LAG(expression [, offset] [, default])`
- `FIRST_VALUE(expression)`
- `LAST_VALUE(expression)`
- `LISTAGG(expression [, separator])`

{{< hint danger >}}
`DATE_FORMAT(timestamp, string)` is available in the legacy planner but has serious bugs and should not be used.
Please implement a custom UDF instead or use `EXTRACT` as a workaround.
{{< /hint >}}


text:
package yaml

import (
	"encoding"
	"encoding/base64"
	"fmt"
	"math"
	"reflect"
	"strconv"
	"time"
)

const (
	documentNode = 1 << iota
	mappingNode
	sequenceNode
	scalarNode
	aliasNode
)

type node struct {
	kind         int
	line, column int
	tag          string
	value        string
	implicit     bool
	children     []*node
	anchors      map[string]*node
}

// ----------------------------------------------------------------------------
// Parser, produces a node tree out of a libyaml event stream.

type parser struct {
	parser yaml_parser_t
	event  yaml_event_t
	doc    *node
}

func newParser(b []byte) *parser {
	p := parser{}
	if !yaml_parser_initialize(&p.parser) {
		panic("failed to initialize YAML emitter")
	}

	if len(b) == 0 {
		b = []byte{'\n'}
	}

	yaml_parser_set_input_string(&p.parser, b)

	p.skip()
	if p.event.typ != yaml_STREAM_START_EVENT {
		panic("expected stream start event, got " + strconv.Itoa(int(p.event.typ)))
	}
	p.skip()
	return &p
}

func (p *parser) destroy() {
	if p.event.typ != yaml_NO_EVENT {
		yaml_event_delete(&p.event)
	}
	yaml_parser_delete(&p.parser)
}

func (p *parser) skip() {
	if p.event.typ != yaml_NO_EVENT {
		if p.event.typ == yaml_STREAM_END_EVENT {
			failf("attempted to go past the end of stream; corrupted value?")
		}
		yaml_event_delete(&p.event)
	}
	if !yaml_parser_parse(&p.parser, &p.event) {
		p.fail()
	}
}

func (p *parser) fail() {
	var where string
	var line int
	if p.parser.problem_mark.line != 0 {
		line = p.parser.problem_mark.line
	} else if p.parser.context_mark.line != 0 {
		line = p.parser.context_mark.line
	}
	if line != 0 {
		where = "line " + strconv.Itoa(line) + ": "
	}
	var msg string
	if len(p.parser.problem) > 0 {
		msg = p.parser.problem
	} else {
		msg = "unknown problem parsing YAML content"
	}
	failf("%s%s", where, msg)
}

func (p *parser) anchor(n *node, anchor []byte) {
	if anchor != nil {
		p.doc.anchors[string(anchor)] = n
	}
}

func (p *parser) parse() *node {
	switch p.event.typ {
	case yaml_SCALAR_EVENT:
		return p.scalar()
	case yaml_ALIAS_EVENT:
		return p.alias()
	case yaml_MAPPING_START_EVENT:
		return p.mapping()
	case yaml_SEQUENCE_START_EVENT:
		return p.sequence()
	case yaml_DOCUMENT_START_EVENT:
		return p.document()
	case yaml_STREAM_END_EVENT:
		// Happens when attempting to decode an empty buffer.
		return nil
	default:
		panic("attempted to parse unknown event: " + strconv.Itoa(int(p.event.typ)))
	}
	panic("unreachable")
}

func (p *parser) node(kind int) *node {
	return &node{
		kind:   kind,
		line:   p.event.start_mark.line,
		column: p.event.start_mark.column,
	}
}

func (p *parser) document() *node {
	n := p.node(documentNode)
	n.anchors = make(map[string]*node)
	p.doc = n
	p.skip()
	n.children = append(n.children, p.parse())
	if p.event.typ != yaml_DOCUMENT_END_EVENT {
		panic("expected end of document event but got " + strconv.Itoa(int(p.event.typ)))
	}
	p.skip()
	return n
}

func (p *parser) alias() *node {
	n := p.node(aliasNode)
	n.value = string(p.event.anchor)
	p.skip()
	return n
}

func (p *parser) scalar() *node {
	n := p.node(scalarNode)
	n.value = string(p.event.value)
	n.tag = string(p.event.tag)
	n.implicit = p.event.implicit
	p.anchor(n, p.event.anchor)
	p.skip()
	return n
}

func (p *parser) sequence() *node {
	n := p.node(sequenceNode)
	p.anchor(n, p.event.anchor)
	p.skip()
	for p.event.typ != yaml_SEQUENCE_END_EVENT {
		n.children = append(n.children, p.parse())
	}
	p.skip()
	return n
}

func (p *parser) mapping() *node {
	n := p.node(mappingNode)
	p.anchor(n, p.event.anchor)
	p.skip()
	for p.event.typ != yaml_MAPPING_END_EVENT {
		n.children = append(n.children, p.parse(), p.parse())
	}
	p.skip()
	return n
}

// ----------------------------------------------------------------------------
// Decoder, unmarshals a node into a provided value.

type decoder struct {
	doc     *node
	aliases map[string]bool
	mapType reflect.Type
	terrors []string
}

var (
	mapItemType       = reflect.TypeOf(MapItem{})
	durationType      = reflect.TypeOf(time.Duration(0))
	defaultMapType    = reflect.TypeOf(map[interface{}]interface{}{})
	originalMapType   = defaultMapType
	jsonCompatMapType = reflect.TypeOf(map[string]interface{}{})
	ifaceType         = defaultMapType.Elem()
)

// PreserveJSONCodecCompatibility changes the default map type used when
// decoding YAML content from  `map[interface{}]interface{}` to
// `map[string]interface{}`.
//
// The Go StdLib JSON marshaller cannot encode maps with keys other than of
// type string. Therefore, maps created by this package's decoder must use a
// a string as the key-type in order for the same maps to be marshaled into
// JSON at a later date.
//
// This function is not safe for concurrent use and should be invoked prior to
// any codec activities.
func PreserveJSONCodecCompatibility(preserve bool) {
	if preserve {
		defaultMapType = jsonCompatMapType
	} else {
		defaultMapType = originalMapType
	}
	ifaceType = defaultMapType.Elem()
}

func newDecoder() *decoder {
	d := &decoder{mapType: defaultMapType}
	d.aliases = make(map[string]bool)
	return d
}

func (d *decoder) terror(n *node, tag string, out reflect.Value) {
	if n.tag != "" {
		tag = n.tag
	}
	value := n.value
	if tag != yaml_SEQ_TAG && tag != yaml_MAP_TAG {
		if len(value) > 10 {
			value = " `" + value[:7] + "...`"
		} else {
			value = " `" + value + "`"
		}
	}
	d.terrors = append(d.terrors, fmt.Sprintf("line %d: cannot unmarshal %s%s into %s", n.line+1, shortTag(tag), value, out.Type()))
}

func (d *decoder) callUnmarshaler(n *node, u Unmarshaler) (good bool) {
	terrlen := len(d.terrors)
	err := u.UnmarshalYAML(func(v interface{}) (err error) {
		defer handleErr(&err)
		d.unmarshal(n, reflect.ValueOf(v))
		if len(d.terrors) > terrlen {
			issues := d.terrors[terrlen:]
			d.terrors = d.terrors[:terrlen]
			return &TypeError{issues}
		}
		return nil
	})
	if e, ok := err.(*TypeError); ok {
		d.terrors = append(d.terrors, e.Errors...)
		return false
	}
	if err != nil {
		fail(err)
	}
	return true
}

// d.prepare initializes and dereferences pointers and calls UnmarshalYAML
// if a value is found to implement it.
// It returns the initialized and dereferenced out value, whether
// unmarshalling was already done by UnmarshalYAML, and if so whether
// its types unmarshalled appropriately.
//
// If n holds a null value, prepare returns before doing anything.
func (d *decoder) prepare(n *node, out reflect.Value) (newout reflect.Value, unmarshaled, good bool) {
	if n.tag == yaml_NULL_TAG || n.kind == scalarNode && n.tag == "" && (n.value == "null" || n.value == "") {
		return out, false, false
	}
	again := true
	for again {
		again = false
		if out.Kind() == reflect.Ptr {
			if out.IsNil() {
				out.Set(reflect.New(out.Type().Elem()))
			}
			out = out.Elem()
			again = true
		}
		if out.CanAddr() {
			if u, ok := out.Addr().Interface().(Unmarshaler); ok {
				good = d.callUnmarshaler(n, u)
				return out, true, good
			}
		}
	}
	return out, false, false
}

func (d *decoder) unmarshal(n *node, out reflect.Value) (good bool) {
	switch n.kind {
	case documentNode:
		return d.document(n, out)
	case aliasNode:
		return d.alias(n, out)
	}
	out, unmarshaled, good := d.prepare(n, out)
	if unmarshaled {
		return good
	}
	switch n.kind {
	case scalarNode:
		good = d.scalar(n, out)
	case mappingNode:
		good = d.mapping(n, out)
	case sequenceNode:
		good = d.sequence(n, out)
	default:
		panic("internal error: unknown node kind: " + strconv.Itoa(n.kind))
	}
	return good
}

func (d *decoder) document(n *node, out reflect.Value) (good bool) {
	if len(n.children) == 1 {
		d.doc = n
		d.unmarshal(n.children[0], out)
		return true
	}
	return false
}

func (d *decoder) alias(n *node, out reflect.Value) (good bool) {
	an, ok := d.doc.anchors[n.value]
	if !ok {
		failf("unknown anchor '%s' referenced", n.value)
	}
	if d.aliases[n.value] {
		failf("anchor '%s' value contains itself", n.value)
	}
	d.aliases[n.value] = true
	good = d.unmarshal(an, out)
	delete(d.aliases, n.value)
	return good
}

var zeroValue reflect.Value

func resetMap(out reflect.Value) {
	for _, k := range out.MapKeys() {
		out.SetMapIndex(k, zeroValue)
	}
}

func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
	var tag string
	var resolved interface{}
	if n.tag == "" && !n.implicit {
		tag = yaml_STR_TAG
		resolved = n.value
	} else {
		tag, resolved = resolve(n.tag, n.value)
		if tag == yaml_BINARY_TAG {
			data, err := base64.StdEncoding.DecodeString(resolved.(string))
			if err != nil {
				failf("!!binary value contains invalid base64 data")
			}
			resolved = string(data)
		}
	}
	if resolved == nil {
		if out.Kind() == reflect.Map && !out.CanAddr() {
			resetMap(out)
		} else {
			out.Set(reflect.Zero(out.Type()))
		}
		return true
	}
	if s, ok := resolved.(string); ok && out.CanAddr() {
		if u, ok := out.Addr().Interface().(encoding.TextUnmarshaler); ok {
			err := u.UnmarshalText([]byte(s))
			if err != nil {
				fail(err)
			}
			return true
		}
	}
	switch out.Kind() {
	case reflect.String:
		if tag == yaml_BINARY_TAG {
			out.SetString(resolved.(string))
			good = true
		} else if resolved != nil {
			out.SetString(n.value)
			good = true
		}
	case reflect.Interface:
		if resolved == nil {
			out.Set(reflect.Zero(out.Type()))
		} else {
			out.Set(reflect.ValueOf(resolved))
		}
		good = true
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		switch resolved := resolved.(type) {
		case int:
			if !out.OverflowInt(int64(resolved)) {
				out.SetInt(int64(resolved))
				good = true
			}
		case int64:
			if !out.OverflowInt(resolved) {
				out.SetInt(resolved)
				good = true
			}
		case uint64:
			if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
				out.SetInt(int64(resolved))
				good = true
			}
		case float64:
			if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
				out.SetInt(int64(resolved))
				good = true
			}
		case string:
			if out.Type() == durationType {
				d, err := time.ParseDuration(resolved)
				if err == nil {
					out.SetInt(int64(d))
					good = true
				}
			}
		}
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		switch resolved := resolved.(type) {
		case int:
			if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
				out.SetUint(uint64(resolved))
				good = true
			}
		case int64:
			if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
				out.SetUint(uint64(resolved))
				good = true
			}
		case uint64:
			if !out.OverflowUint(uint64(resolved)) {
				out.SetUint(uint64(resolved))
				good = true
			}
		case float64:
			if resolved <= math.MaxUint64 && !out.OverflowUint(uint64(resolved)) {
				out.SetUint(uint64(resolved))
				good = true
			}
		}
	case reflect.Bool:
		switch resolved := resolved.(type) {
		case bool:
			out.SetBool(resolved)
			good = true
		}
	case reflect.Float32, reflect.Float64:
		switch resolved := resolved.(type) {
		case int:
			out.SetFloat(float64(resolved))
			good = true
		case int64:
			out.SetFloat(float64(resolved))
			good = true
		case uint64:
			out.SetFloat(float64(resolved))
			good = true
		case float64:
			out.SetFloat(resolved)
			good = true
		}
	case reflect.Ptr:
		if out.Type().Elem() == reflect.TypeOf(resolved) {
			// TODO DOes this make sense? When is out a Ptr except when decoding a nil value?
			elem := reflect.New(out.Type().Elem())
			elem.Elem().Set(reflect.ValueOf(resolved))
			out.Set(elem)
			good = true
		}
	}
	if !good {
		d.terror(n, tag, out)
	}
	return good
}

func settableValueOf(i interface{}) reflect.Value {
	v := reflect.ValueOf(i)
	sv := reflect.New(v.Type()).Elem()
	sv.Set(v)
	return sv
}

func (d *decoder) sequence(n *node, out reflect.Value) (good bool) {
	l := len(n.children)

	var iface reflect.Value
	switch out.Kind() {
	case reflect.Slice:
		out.Set(reflect.MakeSlice(out.Type(), l, l))
	case reflect.Interface:
		// No type hints. Will have to use a generic sequence.
		iface = out
		out = settableValueOf(make([]interface{}, l))
	default:
		d.terror(n, yaml_SEQ_TAG, out)
		return false
	}
	et := out.Type().Elem()

	j := 0
	for i := 0; i < l; i++ {
		e := reflect.New(et).Elem()
		if ok := d.unmarshal(n.children[i], e); ok {
			out.Index(j).Set(e)
			j++
		}
	}
	out.Set(out.Slice(0, j))
	if iface.IsValid() {
		iface.Set(out)
	}
	return true
}

func (d *decoder) mapping(n *node, out reflect.Value) (good bool) {
	switch out.Kind() {
	case reflect.Struct:
		return d.mappingStruct(n, out)
	case reflect.Slice:
		return d.mappingSlice(n, out)
	case reflect.Map:
		// okay
	case reflect.Interface:
		if d.mapType.Kind() == reflect.Map {
			iface := out
			out = reflect.MakeMap(d.mapType)
			iface.Set(out)
		} else {
			slicev := reflect.New(d.mapType).Elem()
			if !d.mappingSlice(n, slicev) {
				return false
			}
			out.Set(slicev)
			return true
		}
	default:
		d.terror(n, yaml_MAP_TAG, out)
		return false
	}
	outt := out.Type()
	kt := outt.Key()
	et := outt.Elem()

	mapType := d.mapType
	if outt.Key() == ifaceType && outt.Elem() == ifaceType {
		d.mapType = outt
	}

	if out.IsNil() {
		out.Set(reflect.MakeMap(outt))
	}
	l := len(n.children)
	for i := 0; i < l; i += 2 {
		if isMerge(n.children[i]) {
			d.merge(n.children[i+1], out)
			continue
		}
		k := reflect.New(kt).Elem()
		if d.unmarshal(n.children[i], k) {
			kkind := k.Kind()
			if kkind == reflect.Interface {
				kkind = k.Elem().Kind()
			}
			if kkind == reflect.Map || kkind == reflect.Slice {
				failf("invalid map key: %#v", k.Interface())
			}
			e := reflect.New(et).Elem()
			if d.unmarshal(n.children[i+1], e) {
				out.SetMapIndex(k, e)
			}
		}
	}
	d.mapType = mapType
	return true
}

func (d *decoder) mappingSlice(n *node, out reflect.Value) (good bool) {
	outt := out.Type()
	if outt.Elem() != mapItemType {
		d.terror(n, yaml_MAP_TAG, out)
		return false
	}

	mapType := d.mapType
	d.mapType = outt

	var slice []MapItem
	var l = len(n.children)
	for i := 0; i < l; i += 2 {
		if isMerge(n.children[i]) {
			d.merge(n.children[i+1], out)
			continue
		}
		item := MapItem{}
		k := reflect.ValueOf(&item.Key).Elem()
		if d.unmarshal(n.children[i], k) {
			v := reflect.ValueOf(&item.Value).Elem()
			if d.unmarshal(n.children[i+1], v) {
				slice = append(slice, item)
			}
		}
	}
	out.Set(reflect.ValueOf(slice))
	d.mapType = mapType
	return true
}

func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) {
	sinfo, err := getStructInfo(out.Type())
	if err != nil {
		panic(err)
	}
	name := settableValueOf("")
	l := len(n.children)

	var inlineMap reflect.Value
	var elemType reflect.Type
	if sinfo.InlineMap != -1 {
		inlineMap = out.Field(sinfo.InlineMap)
		inlineMap.Set(reflect.New(inlineMap.Type()).Elem())
		elemType = inlineMap.Type().Elem()
	}

	for i := 0; i < l; i += 2 {
		ni := n.children[i]
		if isMerge(ni) {
			d.merge(n.children[i+1], out)
			continue
		}
		if !d.unmarshal(ni, name) {
			continue
		}
		if info, ok := sinfo.FieldsMap[name.String()]; ok {
			var field reflect.Value
			if info.Inline == nil {
				field = out.Field(info.Num)
			} else {
				field = out.FieldByIndex(info.Inline)
			}
			d.unmarshal(n.children[i+1], field)
		} else if sinfo.InlineMap != -1 {
			if inlineMap.IsNil() {
				inlineMap.Set(reflect.MakeMap(inlineMap.Type()))
			}
			value := reflect.New(elemType).Elem()
			d.unmarshal(n.children[i+1], value)
			inlineMap.SetMapIndex(name, value)
		}
	}
	return true
}

func failWantMap() {
	failf("map merge requires map or sequence of maps as the value")
}

func (d *decoder) merge(n *node, out reflect.Value) {
	switch n.kind {
	case mappingNode:
		d.unmarshal(n, out)
	case aliasNode:
		an, ok := d.doc.anchors[n.value]
		if ok && an.kind != mappingNode {
			failWantMap()
		}
		d.unmarshal(n, out)
	case sequenceNode:
		// Step backwards as earlier nodes take precedence.
		for i := len(n.children) - 1; i >= 0; i-- {
			ni := n.children[i]
			if ni.kind == aliasNode {
				an, ok := d.doc.anchors[ni.value]
				if ok && an.kind != mappingNode {
					failWantMap()
				}
			} else if ni.kind != mappingNode {
				failWantMap()
			}
			d.unmarshal(ni, out)
		}
	default:
		failWantMap()
	}
}

func isMerge(n *node) bool {
	return n.kind == scalarNode && n.value == "<<" && (n.implicit == true || n.tag == yaml_MERGE_TAG)
}


text:
from flask import render_template, flash, redirect, session, url_for, request, g, jsonify
from flask.ext.login import login_user, logout_user, current_user, login_required
from flask.ext.sqlalchemy import get_debug_queries
from flask.ext.babel import gettext
from app import app, db, lm, oid, babel
from forms import LoginForm, EditForm, PostForm, SearchForm
from models import User, ROLE_USER, ROLE_ADMIN, Post
from datetime import datetime
from emails import follower_notification
from guess_language import guessLanguage
from translate import microsoft_translate
from config import POSTS_PER_PAGE, MAX_SEARCH_RESULTS, LANGUAGES, DATABASE_QUERY_TIMEOUT, WHOOSH_ENABLED

@lm.user_loader
def load_user(id):
    return User.query.get(int(id))

@babel.localeselector
def get_locale():
    return request.accept_languages.best_match(LANGUAGES.keys())

@app.before_request
def before_request():
    g.user = current_user
    if g.user.is_authenticated():
        g.user.last_seen = datetime.utcnow()
        db.session.add(g.user)
        db.session.commit()
        g.search_form = SearchForm()
    g.locale = get_locale()
    g.search_enabled = WHOOSH_ENABLED

@app.after_request
def after_request(response):
    for query in get_debug_queries():
        if query.duration >= DATABASE_QUERY_TIMEOUT:
            app.logger.warning("SLOW QUERY: %s\nParameters: %s\nDuration: %fs\nContext: %s\n" % (query.statement, query.parameters, query.duration, query.context))
    return response

@app.errorhandler(404)
def internal_error(error):
    return render_template('404.html'), 404

@app.errorhandler(500)
def internal_error(error):
    db.session.rollback()
    return render_template('500.html'), 500

@app.route('/', methods = ['GET', 'POST'])
@app.route('/index', methods = ['GET', 'POST'])
@app.route('/index/<int:page>', methods = ['GET', 'POST'])
@login_required
def index(page = 1):
    form = PostForm()
    if form.validate_on_submit():
        language = guessLanguage(form.post.data)
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        post = Post(body = form.post.data,
            timestamp = datetime.utcnow(),
            author = g.user,
            language = language)
        db.session.add(post)
        db.session.commit()
        flash(gettext('Your post is now live!'))
        return redirect(url_for('index'))
    #posts = User(g.user).followed_posts().paginate(page, POSTS_PER_PAGE, False)
    posts = g.user.followed_posts()
    return render_template('index.html',
        title = 'Home',
        form = form,
        posts = posts)

@app.route('/login', methods = ['GET', 'POST'])
@oid.loginhandler
def login():
    if g.user is not None and g.user.is_authenticated():
        return redirect(url_for('index'))
    form = LoginForm()
    if form.validate_on_submit():
        session['remember_me'] = form.remember_me.data
        return oid.try_login(form.openid.data, ask_for = ['nickname', 'email'])
    return render_template('login.html',
        title = 'Sign In',
        form = form,
        providers = app.config['OPENID_PROVIDERS'])

@oid.after_login
def after_login(resp):
    if resp.email is None or resp.email == "":
        flash(gettext('Invalid login. Please try again.'))
        return redirect(url_for('login'))
    user = User.query.filter_by(email = resp.email).first()
    if user is None:
        nickname = resp.nickname
        if nickname is None or nickname == "":
            nickname = resp.email.split('@')[0]
        nickname = User.make_valid_nickname(nickname)
        nickname = User.make_unique_nickname(nickname)
        user = User(nickname = nickname, email = resp.email, role = ROLE_USER)
        db.session.add(user)
        db.session.commit()
        # make the user follow him/herself
        db.session.add(user.follow(user))
        db.session.commit()
    remember_me = False
    if 'remember_me' in session:
        remember_me = session['remember_me']
        session.pop('remember_me', None)
    login_user(user, remember = remember_me)
    return redirect(request.args.get('next') or url_for('index'))

@app.route('/logout')
def logout():
    logout_user()
    return redirect(url_for('index'))

@app.route('/user/<nickname>')
@app.route('/user/<nickname>/<int:page>')
@login_required
def user(nickname, page = 1):
    user = User.query.filter_by(nickname = nickname).first()
    if user == None:
        flash(gettext('User %(nickname)s not found.', nickname = nickname))
        return redirect(url_for('index'))
    posts = user.posts.paginate(page, POSTS_PER_PAGE, False)
    return render_template('user.html',
        user = user,
        posts = posts)

@app.route('/edit', methods = ['GET', 'POST'])
@login_required
def edit():
    form = EditForm(g.user.nickname)
    if form.validate_on_submit():
        g.user.nickname = form.nickname.data
        g.user.about_me = form.about_me.data
        db.session.add(g.user)
        db.session.commit()
        flash(gettext('Your changes have been saved.'))
        return redirect(url_for('edit'))
    elif request.method != "POST":
        form.nickname.data = g.user.nickname
        form.about_me.data = g.user.about_me
    return render_template('edit.html',
        form = form)

@app.route('/follow/<nickname>')
@login_required
def follow(nickname):
    user = User.query.filter_by(nickname = nickname).first()
    if user == None:
        flash('User ' + nickname + ' not found.')
        return redirect(url_for('index'))
    if user == g.user:
        flash(gettext('You can\'t follow yourself!'))
        return redirect(url_for('user', nickname = nickname))
    u = g.user.follow(user)
    if u is None:
        flash(gettext('Cannot follow %(nickname)s.', nickname = nickname))
        return redirect(url_for('user', nickname = nickname))
    db.session.add(u)
    db.session.commit()
    flash(gettext('You are now following %(nickname)s!', nickname = nickname))
    follower_notification(user, g.user)
    return redirect(url_for('user', nickname = nickname))

@app.route('/unfollow/<nickname>')
@login_required
def unfollow(nickname):
    user = User.query.filter_by(nickname = nickname).first()
    if user == None:
        flash('User ' + nickname + ' not found.')
        return redirect(url_for('index'))
    if user == g.user:
        flash(gettext('You can\'t unfollow yourself!'))
        return redirect(url_for('user', nickname = nickname))
    u = g.user.unfollow(user)
    if u is None:
        flash(gettext('Cannot unfollow %(nickname)s.', nickname = nickname))
        return redirect(url_for('user', nickname = nickname))
    db.session.add(u)
    db.session.commit()
    flash(gettext('You have stopped following %(nickname)s.', nickname = nickname))
    return redirect(url_for('user', nickname = nickname))

@app.route('/delete/<int:id>')
@login_required
def delete(id):
    post = Post.query.get(id)
    if post == None:
        flash('Post not found.')
        return redirect(url_for('index'))
    if post.author.id != g.user.id:
        flash('You cannot delete this post.')
        return redirect(url_for('index'))
    db.session.delete(post)
    db.session.commit()
    flash('Your post has been deleted.')
    return redirect(url_for('index'))

@app.route('/search', methods = ['POST'])
@login_required
def search():
    if not g.search_form.validate_on_submit():
        return redirect(url_for('index'))
    return redirect(url_for('search_results', query = g.search_form.search.data))

@app.route('/search_results/<query>')
@login_required
def search_results(query):
    results = Post.query.whoosh_search(query, MAX_SEARCH_RESULTS).all()
    return render_template('search_results.html',
        query = query,
        results = results)

@app.route('/translate', methods = ['POST'])
@login_required
def translate():
    return jsonify({
        'text': microsoft_translate(
            request.form['text'],
            request.form['sourceLang'],
            request.form['destLang']) })


text:
<?php

declare(strict_types = 1);

namespace Sop\ASN1\Type\Primitive;

use Sop\ASN1\Type\PrimitiveString;
use Sop\ASN1\Type\UniversalClass;

/**
 * Implements *GeneralString* type.
 */
class GeneralString extends PrimitiveString
{
    use UniversalClass;

    /**
     * Constructor.
     */
    public function __construct(string $string)
    {
        $this->_typeTag = self::TYPE_GENERAL_STRING;
        parent::__construct($string);
    }

    /**
     * {@inheritdoc}
     */
    protected function _validateString(string $string): bool
    {
        // allow everything
        return true;
    }
}