db

package
v1.2023.2 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 11 Imported by: 1

Documentation

Overview

Package db 提供区域数据文件的相关操作

数据格式:

1:[versions]:{id:name:yearIndex:size{}}

- 1 表示数据格式的版本,采用当前包的 Version 常量;
- versions 表示当前数据文件中的数据支持的年份列表,以逗号分隔;
- id 当前区域的 ID;
- name 当前区域的名称;
- yearIndex 此条数据支持的年份列表,每一个位表示一个年份在 versions 中的索引值;
- size 表示子元素的数量;

Index

Constants

View Source
const Version = 1

Version 数据文件的版本号

Variables

View Source
var ErrIncompatible = errors.New("数据文件版本不兼容")

ErrIncompatible 数据文件版本不兼容

当数据文件中指定的版本号与当前的 Version 不相等时,返回此错误。

Functions

This section is empty.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB 区域数据库信息

func Load

func Load(data []byte, separator string, compress bool, version ...int) (*DB, error)

Load 将数据内容加载至 DB 对象

version 仅加载指定年份的数据,如果为空,则加载所有数据;

func LoadFS added in v0.3.0

func LoadFS(f fs.FS, file, separator string, compress bool, version ...int) (*DB, error)

LoadFS 从数据文件加载数据

func LoadFile

func LoadFile(file, separator string, compress bool, version ...int) (*DB, error)

LoadFile 从数据文件加载数据

func New

func New() *DB

New 返回 DB 的空对象

func Unmarshal deprecated

func Unmarshal(data []byte, separator string, version ...int) (*DB, error)

Unmarshal 解码 data 至 DB

Deprecated: 请使用 Load 代替

func (*DB) AddItem

func (db *DB) AddItem(regionID, name string, ver int) error

AddItem 添加一条子项

func (*DB) AddVersion

func (db *DB) AddVersion(ver int) (ok bool)

AddVersion 添加新的版本号

func (*DB) Dump

func (db *DB) Dump(file string, compress bool) error

Dump 输出到文件

func (*DB) Find

func (db *DB) Find(id ...string) *Region

Find 查找指定 ID 对应的信息

func (*DB) Search

func (db *DB) Search(opt *Options) []*Region

Search 简单的搜索功能

func (*DB) VersionIndex

func (db *DB) VersionIndex(ver int) int

VersionIndex 指定年份在 Versions 中的下标

如果不存在,返回 -1

func (*DB) Versions added in v1.2021.2

func (db *DB) Versions() []int

type Options

type Options struct {
	// 表示你需要搜索的地名需要包含的内容
	//
	// 不能是多个名称的组合,比如"浙江温州",直接写"温州"就可以。
	// 也不要提供类似于"居委会"这种无实际意义的地名;
	Text string

	// 上一级的区域 ID
	//
	// 为空表示不限制。
	Parent string

	// 搜索的城市类型
	//
	// 该值取值于 github.com/issue9/cnregion/id.Level 类型。 多个值可以通过或运算叠加。
	// 0 表示所有类型。
	Level id.Level

	// 最大的搜索数量。0 表示不限制数量。
	Max int
	// contains filtered or unexported fields
}

Options 搜索选项

type Region

type Region struct {
	ID       string
	Name     string
	Items    []*Region
	Versions []int // 支持的版本号列表

	FullName string // 全名
	FullID   string
	// contains filtered or unexported fields
}

Region 表示单个区域

func (*Region) IsSupported

func (reg *Region) IsSupported(ver int) bool

IsSupported 当前数据是否支持该年份

Jump to

Keyboard shortcuts

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